-1

I am trying to build a library in a legacy C++ project. Platform is power-pc using gcc compiler.

The compiler is giving build errors similar to this: error: string: No such file or directory or error: vector: No such file or directory.

  • My understanding is that it is not able to locate the standard library. Where are the standard library files typically reside, and in what format? Is it *.h or some other? I searched for this on internet but I don't think I fully understand it.

  • The confusing part is that another library in the same project using same source code file builds prefectly alright. This suggests to me that may be the makefiles for these two projects are different, where one IS able to locate the std lib, other isn't. But a quick comparison b/w the two didn't bring up any obvious differences. Any other thoughts on this please?

  • Lastly, I just learned that string.h is for c-style strings, and string is for C++ std lib. Is it ok to mix them, i.e. a source file has #include string.h, and also #include string implicitly through including some other file? I ask because this is the same situation in the file that is not building.

Thanks.

Error Message:

Compiling SOURCE_FILE.cpp

In file included from SOURCE_FILE.cpp:3:

HDR_FILE.h:1: error: string: No such file or directory

HDR_FILE.h:2: error: vector: No such file or directory

CODE IN SOURCE_FILE.cpp

#include <stdlib.h>
#include <string.h>
#include "fileABC.h"

using namespace std;

// Other code

CODE IN HRD_FILE.h

#include <string>
#include <vector>
#include <hdr.h>
#include <hdr-cseq.h>
//... OTHER FILES ETC.
sw_eng
  • 129
  • 1
  • 2
  • 7
  • 8
    Just to be sure: you are compiling the C++ code with `g++` and not `gcc`, right? – Mat Feb 28 '12 at 19:56
  • please show a **stripped down version** of your program. – Jörg Beyer Feb 28 '12 at 20:06
  • @Mat: It is compiling with gcc i.e. powerpc-eabi-gcc, but it is the same compiler that successfully built the other library using the same source code file. – sw_eng Feb 28 '12 at 20:09
  • 3
    @sw_eng: use `g++` to compile `C++`, `gcc` to compile `C`. `gcc` will compile _some_ C++ sources if it autodetects the language (based on file extension), but it will not link it properly, and if it mis-detects a file, it will give you all kinds of strange errors, _including not finding standard includes_. – Mat Feb 28 '12 at 20:12
  • Thanks to all who took time to reply. – sw_eng Mar 01 '12 at 13:03

2 Answers2

5

If the files are not being detected as C++ source code then you can get errors like this. Are the c++ files named using an appropriate file extension?

cat test.c

#include <vector>
int main() {}

gcc test.c

test.c:1:18: error: vector: No such file or directory

The command above will compile C code, but not C++. Remember that these are two different languages. To build C++ programs you have to tell the compiler that the source is C++ one way or another. If you use g++ it will automatically assume files with '.c' extensions are C++ (at least my version does. Or you can tell the compiler to use C++ by passing -x c++ right before you pass the file. You also have to tell the linker to include the C++ library. g++ does this automatically as well, or you can pass the linker flag -lstdc++.

The hard way:

gcc -x c++ test.c -lstdc++

The easy way:

g++ test.c

In answer to your last question, you can freely mix <string.h> and <string> (though you should probably use <cstring> instead of <string.h>).

bames53
  • 86,085
  • 15
  • 179
  • 244
  • Thanks. I am considering changing the legacy build scripts to use g++. One thing I did notice that nowhere in the sources (and most of it are cpp files) powerpc-eabi-g++ was used to build - all were built using powerpc-eabi-gcc. Neither, do I see any explicit references to stdc++ or -x c++ in the makefiles. May be it stdc++ was pulled in implicitly though boost (I do see some boost headers in the source code files). Is there I could determine how then some of the other libaries using string, vector etc were able to link into libstdc++ even if it was not explicitly called out in the makefile? – sw_eng Feb 29 '12 at 13:08
  • @sw_eng See if there's a verbose mode for the build scripts so that it will output the commands it is executing. Then see if you can execute those commands by hand, both for the other C++ files and for your own to figure out what settings are needed for C++. – bames53 Feb 29 '12 at 15:11
  • I did change the compiler to g++, but I still get the same error: it is not able to locate string. Interestingly enough in the dependency file for the source file, it DOES show path to folder in cygwin where string resides (/usr/local/lib/gcc/powerpc-eabi/4.1.2/../../../../include/c++/4.1.2/string). Currently I am working on adding verbose flags to see more of what is going under the hood ... – sw_eng Feb 29 '12 at 16:25
  • I eventually figured out that the makefile had the option -no-stdinc++ enabled and so the compiler wasn't looking into standard folders for headers. When I re-enabled, I have now fixed the above errors (though now a new set of errors in the standard headers which in theory should just work out of the box - but that's another story). Thanks for the suggestions. – sw_eng Mar 01 '12 at 13:03
2

The C language string is different than the C++ std::string.

The C language string type is a sequence of characters terminated by a nul, zero, '\0', character. The function declarations for managing this data structure are in <string.h> or <cstring>, depending on whether you compile as C or C++, respectively.

The C++ std::string type is a container of characters. The actual implementation may vary among compilers, but the interface won't. The methods of the std::string are declared in <string>. The compiler must be set up for compiling in the C++ language.

The C++ std::string has methods for generating C language strings and also for creating C++ strings from C language style strings. Read up on the Standard Template Library for more information.

Also, search the Web for "C++ FAQ". This is mandatory reading before programming in the C++ language. There is also a FAQ for the C language too.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154