GCC 默认包含哪些目录?

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

海湾合作委员会如何知道如何找到这些文件?

它内部是否有 /usr/include路径硬连接,或者它将从其他操作系统组件获得路径?

126480 次浏览

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

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

这要归功于 Qt Creator Team

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

$ 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 + + 头版

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

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

尽管我同意 Ihor Kaharlichenko 关于考虑 C + + 的答案,并且同意 Deep.7关于其输出的紧凑性的答案,但是对于 gcc 的多拱版本来说,它们仍然是 不完整,因为输入处理依赖于命令行参数和宏。

例如:

Echo |/opt/gcc-arm-none-eabi-9-2019-q4-main/bin/arm-none-eabi-g + +-specs = nan.specs-mcpu = skin-m4-March = armv7e-m-mumb- mfloat-abi = soft-x c + +-E-Wp,-v 只有 产生

⋮
/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的示例。

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

总结一下其他的答案:

对于 C + + :

c++ -xc++ /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p'

C:

cc -xc /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p'