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

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


当前回答

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

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

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

同样,对于C预处理器:

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

其他回答

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选项后指定的目录。

这些是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.

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

echo $C_INCLUDE_PATH

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

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include
`gcc -print-prog-name=cc1plus` -v

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

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

同样,对于C预处理器:

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