什么是GCC默认包含目录?

时间:2011-02-12 21:45:08

标签: c++ c linux gcc include-path

当我使用gcc编译一个非常简单的源文件时,我不必指定标准包含文件的路径,例如stdio或stdlib。

GCC如何知道如何找到这些文件?

它内部是否有/usr/include路径硬连线,还是从其他操作系统组件获取路径?

5 个答案:

答案 0 :(得分:146)

为了找出gcc / g++使用的默认路径及其优先级,您需要检查以下命令的输出:

  1. C
  2.     gcc -xc -E -v -
    
    1. C ++
    2.     gcc -xc++ -E -v -
      

      归功于 Qt Creator团队

答案 1 :(得分:30)

有一个输出较短的命令,允许从一行开始自动剪切包含的包含:

$ echo | gcc -Wp,-v -x c++ - -fsyntax-only
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/x86_64-redhat-linux
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/../../../../include/c++/4.8.2/backward
 /usr/lib/gcc/x86_64-redhat-linux/4.8.2/include
 /usr/local/include
 /usr/include
End of search list.

归功于libc ++ front-page

答案 2 :(得分:2)

以下是一篇描述gcc搜索路径的文章:http://www.network-theory.co.uk/docs/gccintro/gccintro_21.html

此外,您可以使用-I开关指定额外的搜索路径。您可以从命令行或makefile中执行此操作。

答案 3 :(得分:0)

只需运行以下命令以列出默认搜索路径:

$(gcc -print-prog-name = cc1)-v

答案 4 :(得分:0)

尽管我同意Ihor Kaharlichenko关于考虑C ++的回答,也同意abyss.7关于其输出紧凑性的回答,但对于gcc的多体系结构版本,它们仍然不完整,因为输入处理取决于命令行参数和宏。

示例:

echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -specs=nano.specs -mcpu=cortex-m4 -march=armv7e-m -mthumb -mfloat-abi=soft -x c++ -E -Wp,-v\ - -fsyntax-only产生

⋮
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../arm-none-eabi/include/newlib-nano                                                                                                              
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1                                                                        
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/arm-none-eabi/thumb/v7e-m/nofp                                         
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/backward                                                               
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include                                                                                                            
 /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed                                                                                                      
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include                                                                                  
⋮

echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v - -fsyntax-only产生

⋮
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1                                                                        
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/arm-none-eabi                                                          
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include/c++/9.2.1/backward                                                               
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include                                                                                                            
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/include-fixed                                                                                                      
/opt/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/include                                                                                  
⋮

前一个调用使用newlib(请参见输出的第1和3行),后者使用标准include。列表末尾的公用文件是使用include_next的示例。

底线:打印包含目录时,请始终考虑所有编译器选项