在Unix系统中,gcc在哪里查找头文件?

今天早上我花了一点时间寻找一些系统头文件,所以我认为这将是很好的信息。


当前回答

这些是gcc在默认情况下查找指定头文件的目录(假定头文件包含在chevrons <>中); 1. /usr/local/include/——用于第三方头文件。 2. /usr/include/——用于系统头文件。

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.

其他回答

GCC手册的CPP章节指出头文件可能位于以下目录中。从搜索路径页面:

GCC在几个不同的地方寻找头文件。在一个普通的Unix系统中,如果你没有指示它,它将在#include中查找请求的头文件:

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

对于c++程序,它也会首先查找/usr/include/g++-v3。

此外,gcc将查找在-I选项后指定的目录。

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

这些是gcc在默认情况下查找指定头文件的目录(假定头文件包含在chevrons <>中); 1. /usr/local/include/——用于第三方头文件。 2. /usr/include/——用于系统头文件。

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.

您可以创建一个试图包含伪系统头的文件。 如果在这样的源上以详细模式运行gcc,它将在查找伪标头时列出所有系统包含位置。

$ echo "#include <bogus.h>" > 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