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

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


当前回答

您可以创建一个试图包含伪系统头的文件。 如果在这样的源上以详细模式运行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

其他回答

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

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

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

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

这些是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打印出它将在其中查找系统头文件的完整目录集,像这样调用它:

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

哪个将产生表单的输出

#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.

如果在命令行上有-I-family选项,它们将影响输出的内容。

(sed命令是为了清除调用打印的所有其他垃圾,LC_ALL=C是为了确保sed命令正常工作——“从这里开始”和“搜索列表结束”短语被翻译为IIRC。)

`gcc -print-prog-name=cc1plus` -v

这个命令会问gcc它正在使用哪个c++预处理器,然后问这个预处理器在哪里查找include。

对于特定的设置,您将得到一个可靠的答案。

同样,对于C预处理器:

`gcc -print-prog-name=cpp` -v

你可以通过查看以下命令查看bash中C程序的(额外的)include路径:

echo $C_INCLUDE_PATH

如果这是空的,它可以被修改为添加默认的包含位置,通过:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include