Linux 什么是 GCC 默认包含目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4980819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the GCC default include directories?
提问by Raxvan
When I compile a very simple source file with gcc I don't have to specify the path to standard include files such as stdio or stdlib.
当我用 gcc 编译一个非常简单的源文件时,我不必指定标准包含文件(如 stdio 或 stdlib)的路径。
How does GCC know how to find these files?
GCC 如何知道如何找到这些文件?
Does it have the /usr/include
path hardwired inside, or it will get the paths from other OS components?
它/usr/include
内部是否有硬连线的路径,或者它会从其他操作系统组件获取路径?
采纳答案by Ihor Kaharlichenko
In order to figure out the default paths used by gcc
/g++
, as well as their priorities, you need to examine the output of the following commands:
为了找出gcc
/使用的默认路径g++
及其优先级,您需要检查以下命令的输出:
- For C:
- 对于C:
gcc -xc -E -v -
- For C++:
- 对于C++:
gcc -xc++ -E -v -
The credit goes to Qt Creator team.
归功于Qt Creator 团队。
回答by abyss.7
There is a command with a shorter output, which allows to automatically cut the include pathes from lines, starting with a single space:
有一个输出较短的命令,它允许自动从行中剪切包含路径,从一个空格开始:
$ 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.
The credit goes to the libc++ front-page.
功劳归于 libc++ front-page。
回答by Daniele Testa
Just run the following to list the default search paths:
只需运行以下命令即可列出默认搜索路径:
$(gcc -print-prog-name=cc1) -v
回答by hermannk
Though I agree with Ihor Kaharlichenko's answer for considering C++ and with abyss.7's answer for the compactness of its output, they are still incompletefor the multi-arch versions of gcc because input processing depends on the command line parameters and macros.
尽管我同意 Ihor Kaharlichenko 考虑 C++ 的回答以及 abyss.7 对其输出紧凑性的回答,但对于 gcc 的多架构版本,它们仍然不完整,因为输入处理取决于命令行参数和宏。
Example:
例子:
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
yields
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
?
whereas echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v - -fsyntax-only
yields
而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
?
The former invocation utilizes newlib
(see lines 1 and 3 of the output), the latter goes with the standard includes. The common files at the end of the list are an example for the usage of include_next
.
前者调用利用newlib
(见输出的第 1 和第 3 行),后者与标准包含一起使用。列表末尾的公共文件是include_next
.
Bottom line: Always consider all macrosand compiler optionswhen printing the include directories.
底线:在打印包含目录时始终考虑所有宏和编译器选项。