GCC 默认包含目录是什么?
当我使用 gcc 编译一个非常简单的源文件时,我不必指定标准包含文件(例如 stdio 或 stdlib)的路径。
GCC 如何知道如何找到这些文件?
它是否有硬连线的 /usr/include
路径,或者它会从其他操作系统组件获取路径?
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.
How does GCC know how to find these files?
Does it have the /usr/include
path hardwired inside, or it will get the paths from other OS components?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了找出
gcc
/g++
使用的默认路径及其优先级,您需要检查以下命令的输出:功劳归于Qt Creator 团队。
以下是标志的详细说明:
-x
分别选择语言,C
或C++
-E
使 gcc 仅运行预处理器,因此不会进行编译-v
打印所有运行的命令,这是转储标准路径的关键< /p>-
是要预处理的“输入文件”,作为约定-
代表 stdin(或 stdout,取决于上下文);echo |
向gcc
提供一个空字符串,因此我们可以有效地预处理动态生成的空文件这里有一个更详细的解释:https://explainshell.com/explain?cmd=echo+%7C+gcc+-xc+-E+- v+-
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:The credit goes to Qt Creator team.
Here's a breakdown of the flags:
-x
selects the language,C
orC++
respectively-E
makes gcc to run the preprocessor only, so no compilation takes place-v
prints all the commands run, which is the key to dumping the standard paths-
is the "input file" to preprocess, as a convention-
stands for stdin (or stdout, depending on the context);echo |
feeds an empty string togcc
so effectively we preprocess an empty file generated on the flyHere's a nice explaining it in more detail: https://explainshell.com/explain?cmd=echo+%7C+gcc+-xc+-E+-v+-
有一个具有较短输出的命令,它允许自动从行中剪切包含路径,以单个空格开头:
功劳归于 libc++ front -页面。
There is a command with a shorter output, which allows to automatically cut the include pathes from lines, starting with a single space:
The credit goes to the libc++ front-page.
总结其他答案:
对于 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'
To summarise the other answers:
For C++:
c++ -xc++ /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p'
For C:
cc -xc /dev/null -E -Wp,-v 2>&1 | sed -n 's,^ ,,p'
尽管我同意 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=软-x c++ -E -Wp,-v\
产生,- -fsyntax-only
而
echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v - -fsyntax-only
产生前者调用使用
newlib
(参见输出的第 1 行和第 3 行),后者与标准包含一致。列表末尾的常见文件是include_next
的使用示例。底线:在打印包含目录时,始终考虑所有宏和编译器选项。
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 incomplete for the multi-arch versions of gcc because input processing depends on the command line parameters and macros.
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\
yields- -fsyntax-only
whereas
echo | /opt/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-g++ -x c++ -E -Wp,-v - -fsyntax-only
yieldsThe 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 ofinclude_next
.Bottom line: Always consider all macros and compiler options when printing the include directories.
只需运行以下命令即可列出默认搜索路径:
Just run the following to list the default search paths: