tags:

views:

1759

answers:

4

On a Unix system, where does gcc look for header files?

I spent a little time this morning looking for some system header files, so I thought this would be good information to have here.

+7  A: 

The CPP Section of the GCC Manual indicates that header files may be located in the following directories:

GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will look for headers requested with #include in:

 /usr/local/include
 libdir/gcc/target/version/include
 /usr/target/include
 /usr/include

For C++ programs, it will also look in /usr/include/g++-v3, first.

Bill the Lizard
What's with the ask a simple question that you answer yourself?
he_the_great
Read the entire question. This information wasn't on SO before. Besides, I got better answers than my own out of it.
Bill the Lizard
he_the_great, what is with it?? there is nothing with it. he has the right to do so and i think it's useful to have this in SO. if the community thinks he just wants to gather reputation, he will let him feel so. but the community (and myself) think he really wants to help SO with useful information
Johannes Schaub - litb
and, beside, it's not a matter of simple or not simple. it's a matter of how many people will have problems with it.
Johannes Schaub - litb
That's fine for your current version of gcc. The actual directories it looks in depends on the options specified when gcc was built. See Shmoopty answer for a better solution.
Martin York
PS: My C++ header files are in: /usr/include/c++/4.0.0
Martin York
@Martin: You're old school. Mine are in /usr/include/c++/4.2 :)
Bill the Lizard
+5  A: 

In addition, gcc will look in the directories specified after the -I option

robert
Thank you. That's good information to add.
Bill the Lizard
+2  A: 

You can create a file that attempts to include a bogus system header. If you run gcc in verbose mode on such a source, it will list all the system include locations as it looks for the bogus header.

$ echo "#include <bogus.h> int main(){}" > t.c; gcc -v t.c; rm t.c

[..]

#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i686-apple-darwin9/4.0.1/include
 /usr/include
 /System/Library/Frameworks (framework directory)
 /Library/Frameworks (framework directory)
End of search list.

[..]

t.c:1:32: error: bogus.h: No such file or directory
diciu
I think this would be more helpful if you just said "use the -v option".
Jay Conrod
Well if you use "-v" without a C file that includes a non-existent system header you will not cause gcc to iterate through all the include paths.The key to my answer is bogus.h listed as a system header.
diciu
@Jay - you're right, it was too vague - I've explained what I was doing in the shell script.
diciu
@Jay: The -v option by itself gives output that doesn't include system path.
Bill the Lizard
@diciu: Nice answer. :)
Bill the Lizard
+13  A: 
`gcc -print-prog-name=cc1plus` -v

This command asks gcc which C++ preprocessor it is using, and then asks that preprocessor where it looks for includes.

You will get a reliable answer for your specific setup.

Likewise, for the C preprocessor:

`gcc -print-prog-name=cc1` -v
Shmoopty
Nice. This gives the almost the same output as diciu's answer, but has a smaller command line. :)
Bill the Lizard