Finding out what the GCC include path is

CLinuxGccC Preprocessor

C Problem Overview


I'm trying to programmatically find the #include path on Linux, which as I understand it, in practice means finding what GCC considers it to be. (Is that quite true? How does Clang do it?)

According to http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html some of the components involve the CPU architecture and the GCC version; the latter in particular seems tricky; I suppose it could be obtained by running gcc --version and parsing the output (or gcc -v), but this seems inelegant at best and fragile at worst. Doing it from within one's code assuming one's program is being compiled with GCC might be another option, but it would require depending on that assumption.

What's the recommended way to do it?

C Solutions


Solution 1 - C

The command

echo | gcc -E -Wp,-v -

will show the include path in use.

Solution 2 - C

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrwallaceView Question on Stackoverflow
Solution 1 - CcafView Answer on Stackoverflow
Solution 2 - CdevnullView Answer on Stackoverflow