GCC的默认包含目录是什么?


131

当我使用gcc编译非常简单的源文件时,无需指定标准包含文件(例如stdio或stdlib)的路径。

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

/usr/include内部是否具有硬连线的路径,还是会从其他OS组件获取路径?

Answers:


188

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

  1. 对于C
    gcc -xc -E -v -
  1. 对于C ++
    gcc -xc++ -E -v -

这归功于Qt Creator团队


5
cpp -v /dev/null -o /dev/null
Nehal J Wani

36

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

$ 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 ++ 主页


1

尽管我同意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_next

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


1

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

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

这没有列出包含路径。只有程序路径和库路径
kayahr

cc1通常不能直接调用,因为它是一个不在PATH上的内部命令。也许您想编辑答案以描述在哪里找到文件。
卡亚尔
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.