gcc在哪里寻找C和c++头文件呢?

时间:2021-12-07 15:06:55

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

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

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

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

9 个解决方案

#1


181  

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

这个命令询问gcc使用哪个c++预处理器,然后询问它寻找包含的预处理器。

You will get a reliable answer for your specific setup.

你会得到一个可靠的答案,为你的具体设置。

Likewise, for the C preprocessor:

同样,对于C预处理器:

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

#2


26  

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


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

#3


23  

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.

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

$ 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

#4


16  

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

GCC手册的CPP部分指出头文件可能位于以下目录中:

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:

GCC在几个不同的地方查找标题。在正常的Unix系统中,如果您不指示它,它将查找请求的头信息,包括:

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

对于c++程序,它还将首先查看/usr/include/g++-v3。

#5


5  

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

为了让GCC打印出完整的目录集,它将在其中查找系统头,这样调用它:

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

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

(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.)

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

#6


4  

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

#7


2  

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

编译器查找头文件的路径集可以由命令来检查:-。

cpp -v

cpp - v

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.

如果您声明#include“”,编译器首先在源文件的当前目录中搜索,如果没有找到,则继续在上面检索的目录中进行搜索。

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

如果您声明#include <>,则编译器将直接在从上述命令获得的目录中进行搜索。

Source:- http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026

来源:http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026

#8


0  

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

可以通过查看以下内容查看(附加的)C程序的路径:

echo $C_INCLUDE_PATH

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

如果这是空的,可以修改为添加默认值,包括位置:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include

#9


0  

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.

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

如果您决定将自定义头文件放在除上述目录之外的其他地方,您可以将其包括如下:1。使用文件路径引用("./custom_header_files/foo.h"),而不是include语句中的chevrons。2。在编译代码时使用-I开关。我/home/user/custom_headers/ -c foo。c - p foo。基本上,i开关告诉编译器先查看指定的目录-I - switch(在它检查标准目录之前)。当使用-I开关时,头文件可能包括使用chevrons。

#1


181  

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

这个命令询问gcc使用哪个c++预处理器,然后询问它寻找包含的预处理器。

You will get a reliable answer for your specific setup.

你会得到一个可靠的答案,为你的具体设置。

Likewise, for the C preprocessor:

同样,对于C预处理器:

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

#2


26  

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


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

#3


23  

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.

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

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

#4


16  

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

GCC手册的CPP部分指出头文件可能位于以下目录中:

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:

GCC在几个不同的地方查找标题。在正常的Unix系统中,如果您不指示它,它将查找请求的头信息,包括:

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

对于c++程序,它还将首先查看/usr/include/g++-v3。

#5


5  

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

为了让GCC打印出完整的目录集,它将在其中查找系统头,这样调用它:

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

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

(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.)

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

#6


4  

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

#7


2  

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

编译器查找头文件的路径集可以由命令来检查:-。

cpp -v

cpp - v

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.

如果您声明#include“”,编译器首先在源文件的当前目录中搜索,如果没有找到,则继续在上面检索的目录中进行搜索。

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

如果您声明#include <>,则编译器将直接在从上述命令获得的目录中进行搜索。

Source:- http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026

来源:http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art026

#8


0  

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

可以通过查看以下内容查看(附加的)C程序的路径:

echo $C_INCLUDE_PATH

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

如果这是空的,可以修改为添加默认值,包括位置:

export C_INCLUDE_PATH=$C_INCLUDE_PATH:/usr/include

#9


0  

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.

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

如果您决定将自定义头文件放在除上述目录之外的其他地方,您可以将其包括如下:1。使用文件路径引用("./custom_header_files/foo.h"),而不是include语句中的chevrons。2。在编译代码时使用-I开关。我/home/user/custom_headers/ -c foo。c - p foo。基本上,i开关告诉编译器先查看指定的目录-I - switch(在它检查标准目录之前)。当使用-I开关时,头文件可能包括使用chevrons。