找出 GCC 包含路径是什么

我试图通过编程方式在 Linux 上找到 #include路径,据我所知,这实际上意味着找到 GCC 所认为的 #include路径。(这是真的吗?Clang 是怎么做到的?)

根据 http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html,一些组件涉及 CPU 架构和 GCC 版本; 后者似乎特别棘手; 我认为它可以通过运行 gcc --version和解析输出(或 gcc -v)来获得,但是这看起来最好的情况下是不优雅的,最坏的情况下是脆弱的。假设程序是用 GCC 编译的,那么从代码中进行编译可能是另一种选择,但这需要依赖于这种假设。

推荐的方法是什么?

121656 次浏览

I'm not sure what you mean by the recommended way to find the include path. The standard way is as given below (for c and c++):

$ `gcc -print-prog-name=cc1` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
^C
$ `gcc -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
^C

The command

echo | gcc -E -Wp,-v -

will show the include path in use.