preprocessor


Obtaining the include paths cpp searches


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.
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.
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.
After some chat to get context, I think this question is just a misunderstanding of cpp for parsing different languages.
I don't know how portable -x is (or how portable -v's output format is, for that matter), though other compilers could have something very similar (I believe Intel's compiler behaves identically here, for example), but it seems you just need to tell cpp what language you're using for it to include it's language-dependent, internally-configured paths:
$ cpp --version # my cpp is from gcc
cpp (Ubuntu 4.4.3-4ubuntu5) 4.4.3
...
$ cpp -v </dev/null 2>&1 1>/dev/null | sed -nr 's/^ ([^ ]+)$/\1/p'
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
/usr/include/i486-linux-gnu
/usr/include
$ cpp -v -x c++ </dev/null 2>&1 1>/dev/null | sed -nr 's/^ ([^ ]+)$/\1/p'
/usr/include/c++/4.4
/usr/include/c++/4.4/i486-linux-gnu
/usr/include/c++/4.4/backward
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
/usr/include/i486-linux-gnu
/usr/include
This dovetails nicely when your script takes project-specific include paths:
$ mkdir my-include # or else cpp ignores it
$ cpp -Imy-include -v -x c++ </dev/null 2>&1 1>/dev/null | sed -nr 's/^ ([^ ]+)$/\1/p'
my-include
/usr/include/c++/4.4
/usr/include/c++/4.4/i486-linux-gnu
/usr/include/c++/4.4/backward
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include
/usr/lib/gcc/i486-linux-gnu/4.4.3/include-fixed
/usr/include/i486-linux-gnu
/usr/include
The returned order is the order to search, however <> includes skip the paths for "" includes (but "" includes do search <> paths). Here, cpp's output does distinguish the two sets of paths, if you need that.

Related Links

Preprocessor assertions in arduino
expanding a macro within a macro definition
GNU-M4: Strip empty lines
Escaping space in OpenCL compiler arguments
Does Corona have preprocessor statements so I can execute Lua code for specific devices?
Preprocessing in C++
Truncate string in cpp (preprocessor)
C++ precompiler conditionally include code
DDK preprocessor directives
Preprocessor, Postprocessor, Geometric Processor and NC processor
Gfortran pre-processor directives for Different Operating systems
Good data set for Pre-processing
IDE inserts incorrect #include statement
Save string in C preprocessor
Fortran preprocessing with Portland compiler
Making a macro expansion dependent on its arguments

Categories

HOME
fabricjs
prism
cheerio
amp-html
interface
can
simulink
hugo
special-characters
reactive-programming
fhir
github-enterprise
supervisor
symbol
package.json
android-7.0-nougat
vuejs
google-content-api
identity-management
connector
swagger-2.0
apache2.4
drive
zxing
windows-server-2003
angularjs-components
bioinformatics
jaws
corenlp-server
zend-debugger
testbed
papaparse
getlasterror
node-mssql
resolution
apollostack
http-request
importerror
x++
netbeans6.8
twilio-click-to-call
tf
page-replacement
aquamacs
er-diagram
cd-burning
smartfoxserver
chromium-os
omxplayer
redbeard-ios
seek
tinkerpop3
mongodb-php
jboss-tools
pre-build-event
cfchart
phpgrid
asyncdisplaykit
boomerang
gocql
rubaxa-sortable
mod-proxy
zscript
matlab-deployment
angular-gettext
iiop
primitive-types
genymotion-call
angular-carousel
ora-00904
live555
tinyxml
jquery-slider
spring-remoting
websphere-esb
android-usb
spring-3
jquery-1.9
diem
frameworkelementfactory
wcf-configuration
fitbounds
jquery-address
fsevents
android-holo-everywhere
tournament
photolibrary
feasibility
dsl-tools
flvplayback

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App