112

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?

0

2 Answers 2

215

The command

echo | gcc -E -Wp,-v - 

will show the include path in use.

Sign up to request clarification or add additional context in comments.

6 Comments

does which gcc command on bash do the same stuff?
Is the echo | necessary?
The cpp -Wp,-v command waits for some input. The echo | saves the user needing Ctrl + D or Ctrl + C to return to shell.
echo | cpp -xc++ -Wp,-v would give the C++ include paths.
gcc -E -Wp,-v -xc /dev/null <- This version does not require any shell (Use g++ -E -Wp,-v -xc++ /dev/null for C++)
|
29

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 

2 Comments

So, which one is the exact path?
@jay C and C++ have different include paths, pay attention to prog-name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.