Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ default header include list

While performing a compilation with cross g++ in a Linux machine ( lubuntu 11.10 ) in verbose mode, I can see the list of the default include header directories:

#include <...> search starts here:
/opt/eldk-4.2/usr/bin/../lib/gcc/powerpc-linux/4.2.2/include
/opt/eldk-4.2/ppc_4xx/usr/include/c++/4.2.2/opt/eldk-4.2/ppc_4xx/usr/include/c++/4.2.2/powerpc-linux
/opt/eldk-4.2/ppc_4xx/usr/include/c++/4.2.2/backward
/opt/eldk-4.2/usr/../ppc_4xx/usr/include

Executing the very same g++ binary in another Linux machine (lubuntu 12.10), I get another different list, with less elements:

#include <...> search starts here:
/opt/eldk-4.2/usr/bin/../lib/gcc/powerpc-linux/4.2.2/include

and in which some of the elments seem bad constructed, like the following:

ignoring nonexistent directory "/opt/ppc_4xx/usr/lib/gcc/powerpc-linux/includ../include/c++/4.2.2"

The result is that some code compiling on the first system is not compiling on the second because some headers are not found.

Why is this happening?. Where does this list come from?. Who is responsible for constructing it?. Is it possible to easily change it?.

Any help is appreciated.

like image 975
Marda Avatar asked Jun 17 '12 18:06

Marda


People also ask

Where does #include look for files?

If you declare #include "" , the compiler first searches in current directory of source file and if not found, continues to search in the above retrieved directories.

Where does GCC look for include files?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets.

Where do I find header files?

Most standard headers are stored in /usr/include .

Should includes go in header or CPP?

Your #include s should be of header files, and each file (source or header) should #include the header files it needs. Header files should #include the minimum header files necessary, and source files should also, though it's not as important for source files.


1 Answers

You can add directories to the default search path by setting environment variables:

  • C_INCLUDE_PATH (for C header files)
  • CPLUS_INCLUDE_PATH (for C++ header files).

Alternatively, you can create and edit specfile and place it where G++ looks for them. You can check the path with strace gcc.

Additional documentation on specfiles on GCC page.

like image 161
Rafał Rawicki Avatar answered Sep 28 '22 06:09

Rafał Rawicki