Linux find header files

Where does gcc look for C and C++ header files?

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.

By the way, if you want to know where the .h file is location, see stackoverflow.com/a/18593344/1586797

10 Answers 10

`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:

I guess the C preprocessor is cpp instead of cc1 ? On my debian jessie $(gcc -print-prog-name=cpp) -v (correctly) gives one more path, which is /usr/include/x86_64-linux-gnu

If you want that to not hang waiting for input, redirect input from /dev/null , so `gcc -print-prog-name=cc1` -v < /dev/null .

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

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 " > 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 

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.

gcc -v -E — < /dev/null or cpp -v < /dev/null are enough. You just have to get the preprocessor to run, it doesn’t matter what input it sees. (The search paths are printed during startup, before it even looks at its input.)

The CPP Section of the GCC Manual indicates that header files may be located in the following directories. From the Search Path page:

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.

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.

Читайте также:  Arch linux intel graphics drivers

@Base64__ Yes, you can arrange header files in subdirectories under /usr/include and they will be found.

To get GCC to print out the complete set of directories where it will look for system headers, invoke it like this:

$ LC_ALL=C gcc -v -E -xc - < /dev/null 2>&1 | LC_ALL=C sed -ne '/starts here/,/End of/p' 

which will produce output of the form

#include ". " search starts here: #include search starts here: /usr/lib/gcc/x86_64-linux-gnu/5/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed /usr/include/x86_64-linux-gnu /usr/include End of search list. 

If you have -I -family options on the command line they will affect what is printed out.

(The sed command is to get rid of all the other junk this invocation prints, and the LC_ALL=C is to ensure that the sed command works — the «starts here» and «End of search list» phrases are translated IIRC.)

g++ -print-search-dirs gcc -print-search-dirs 

These commands print the default search paths for link libraries and internal components of the compiler; they don’t tell you anything about header files.

The set of paths where the compiler looks for the header files can be checked by the command:-

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.

If you declare #include <> , the compiler searches directly in those directories obtained from the above command.

One could view the (additional) include path for a C program from bash by checking out the following:

If this is empty, it could be modified to add default include locations, by:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include 

These are the directories that gcc looks in by default for the specified header files ( given that the header files are included in chevrons <>); 1. /usr/local/include/ —used for 3rd party header files. 2. /usr/include/ — used for system header files.

Читайте также:  Start bin file linux

If in case you decide to put your custom header file in a place other than the above mentioned directories, you can include them as follows: 1. using quotes («./custom_header_files/foo.h») with files path, instead of chevrons in the include statement. 2. using the -I switch when compiling the code. gcc -I /home/user/custom_headers/ -c foo.c -p foo.o Basically the -I switch tells the compiler to first look in the directory specified with the -I switch ( before it checks the standard directories).When using the -I switch the header files may be included using chevrons.

My system has gcc9 by default and I have built a gcc12 from source and I think the accepted answer is not correct, that is gcc -print-prog-name=cc1plus -v doesn’t give the real include search path.

My build configuration is

Configured with: /home/tian/playground/gcc_build_play/objdir/../gcc-12.1.0/configure --prefix=/home/tian/GCC-12.1.0 --disable-multilib 

And no matter where I mv the gcc12 directoy in my machine. It can always include its own c++ header files correctly.

If I type ./gcc -print-prog-name=cc1plus -v , in the original installed directory, it gives:

tian@tian-B250M-Wind:~/GCC-12.1.0/bin$ `./gcc -print-prog-name=cc1plus` -v ignoring nonexistent directory "/home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include" #include ". " search starts here: #include search starts here: /home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0 /home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/x86_64-pc-linux-gnu /home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/backward /home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/include /usr/local/include /home/tian/GCC-12.1.0/include /home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed /usr/include End of search list. 

mv my gcc12 to ~/Desktop/ , run again, gives:

tian@tian-B250M-Wind:~/Desktop/GCC-12.1.0/bin$ `./gcc -print-prog-name=cc1plus` -v ignoring nonexistent directory "/home/tian/GCC-12.1.0/include/c++/12.1.0" ignoring nonexistent directory "/home/tian/GCC-12.1.0/include/c++/12.1.0/x86_64-pc-linux-gnu" ignoring nonexistent directory "/home/tian/GCC-12.1.0/include/c++/12.1.0/backward" ignoring nonexistent directory "/home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/include" ignoring nonexistent directory "/home/tian/GCC-12.1.0/include" ignoring nonexistent directory "/home/tian/GCC-12.1.0/lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed" ignoring nonexistent directory "/home/tian/GCC-12.1.0/x86_64-pc-linux-gnu/include" #include ". " search starts here: #include search starts here: /usr/local/include /usr/include End of search list. 

If that’s true, then I compile a program with ./g++ , it should use c++ header files in /usr/include or /usr/local/include . But it’s not.

Experiment is omitted here. You can try using mv to rename any header file of gcc12 your test program use or add some garbage code to the header file. Then you will see the gcc12 ./g++ is complaining about the gcc12 c++ header file, not my system gcc9’s c++ header files in /usr/include or /usr/local/include .

So in both places ./g++ can find its gcc12 c++ headers files correctly.

Читайте также:  Linux server windows updates

So I guess gcc and g++ are finding headers in relative directory, relative to /path_to_gcc12/bin/gcc .

Try ./g++ -g -Wall —verbose -o test test.cpp gives the real include path:

tian@tian-B250M-Wind:~/Desktop/GCC-12.1.0/bin$ ./g++ -g -Wall --verbose -o test test.cpp Using built-in specs. COLLECT_GCC=./g++ COLLECT_LTO_WRAPPER=/home/tian/Desktop/GCC-12.1.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.1.0/lto-wrapper Target: x86_64-pc-linux-gnu Configured with: /home/tian/playground/gcc_build_play/objdir/../gcc-12.1.0/configure --prefix=/home/tian/GCC-12.1.0 --disable-multilib Thread model: posix Supported LTO compression algorithms: zlib gcc version 12.1.0 (GCC) COLLECT_GCC_OPTIONS='-g' '-Wall' '-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64' /home/tian/Desktop/GCC-12.1.0/bin/../libexec/gcc/x86_64-pc-linux-gnu/12.1.0/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -iprefix /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/ -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -dumpbase-ext .cpp -mtune=generic -march=x86-64 -g -Wall -version -o /tmp/ccrg0qhG.s GNU C++17 (GCC) version 12.1.0 (x86_64-pc-linux-gnu) compiled by GNU C version 12.1.0, GMP version 6.2.1, MPFR version 4.1.0, MPC version 1.2.1, isl version isl-0.24-GMP GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 ignoring nonexistent directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include" ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0" ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/x86_64-pc-linux-gnu" ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/backward" ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include" ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring duplicate directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed" ignoring nonexistent directory "/home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../x86_64-pc-linux-gnu/include" #include ". " search starts here: #include search starts here: /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0 /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/x86_64-pc-linux-gnu /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/../../../../include/c++/12.1.0/backward /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/x86_64-pc-linux-gnu/12.1.0/include-fixed /usr/local/include /home/tian/Desktop/GCC-12.1.0/bin/../lib/gcc/../../include /usr/include/x86_64-linux-gnu /usr/include End of search list. 

So I think my guess is correct.

Источник

How can I find the header files of the C programming language in Linux?

When I write C programs in Linux, and then compile them using gcc, I am always curious about where those header files are. For example, where stdio.h is. More generally, where is stdbool.h ? What I want to know is not only where it is, but also how to get those places, for example, using shell command or using the C programming language.

10 Answers 10

gcc -H . will print the full path of every include file as a side-effect of regular compilation. Use -fsyntax-only in addition to get it not to create any output (it will still tell you if your program has errors). Example (Linux, gcc-4.7):

$ cat > test.c #include #include ^D $ gcc -H -fsyntax-only test.c . /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdbool.h . /usr/include/stdio.h .. /usr/include/features.h . /usr/include/x86_64-linux-gnu/bits/predefs.h . /usr/include/x86_64-linux-gnu/sys/cdefs.h . /usr/include/x86_64-linux-gnu/bits/wordsize.h . /usr/include/x86_64-linux-gnu/gnu/stubs.h . /usr/include/x86_64-linux-gnu/bits/wordsize.h . /usr/include/x86_64-linux-gnu/gnu/stubs-64.h .. /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h .. /usr/include/x86_64-linux-gnu/bits/types.h . /usr/include/x86_64-linux-gnu/bits/wordsize.h . /usr/include/x86_64-linux-gnu/bits/typesizes.h .. /usr/include/libio.h . /usr/include/_G_config.h . /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stddef.h . /usr/include/wchar.h . /usr/lib/gcc/x86_64-linux-gnu/4.7/include/stdarg.h .. /usr/include/x86_64-linux-gnu/bits/stdio_lim.h .. /usr/include/x86_64-linux-gnu/bits/sys_errlist.h 

The dots at the beginning of each line count how deeply nested the #include is.

Источник

Оцените статью
Adblock
detector