tags:

views:

82

answers:

3

How do I obtain the system include search paths of the C preprocessor? This is for a script that parses arbitrary source files and needs to know the full pathnames of the headers they #include. Let's ignore for a moment that the user can alter this sequence of search paths with compiler flags. I'd prefer a solution that uses standard tools found on POSIX systems so my script depends on next to nothing.

I tried

cpp -v </dev/null |unusually_complex_filter

but this apparently doesn't take in account things like $C{,PLUS}_INCLUDE_PATH. To know where vector of #include<vector> is in I suppose I must know the search paths in their precise order.

A: 

The standard headers are not required to be accessible as regular files containing standard-compliant C source. Usually, they are accessible as files but use many extensions.

Perhaps you can run the preprocessor, which is accessible in a POSIX-compliant manner as c99 -E, on the source files and use its output. POSIX does not define the exact output of the preprocessor, but it usually contains special lines that show the origin of each actual line.

jilles
A: 

On Linux and other POSIX systems, it's /usr/include. If you want to know where vector is, why don't you try with find /usr/include | grep vector$.

I found mine in /usr/include/c++/4.4/vector

djechelon
A: 

with test.cpp like this:

#include <string>
#include <iostream>

int main(int, char **)
{
  return 0
}

and cpp from the gcc toolsuite you can call:

cpp test.cpp | grep '^#.*' | awk '{print $3}'

you will get

"test.cpp"
"<built-in>"
"<command-line>"
"test.cpp"
"/usr/include/c++/4.4/string"
"/usr/include/c++/4.4/string"
"/usr/include/c++/4.4/string"
"/usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h"    
"/usr/include/c++/4.4/x86_64-linux-gnu/bits/c++config.h"
"/usr/include/c++/4.4/x86_64-linux-gnu/bits/os_defines.h"

and many more lines.

You will obviously get a lot of "duplicates", as many include files are included by other include files as well.

lothar