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

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
How to set ISPP defines based on default Inno Setup variables?
standalone C++ preprocessor
Annotations in OCaml
Obtaining the include paths cpp searches
Test for empty macro definition
m4 does not obey expansion?
Detect compilation on Bada OS
Which of the C-family languages have a preprocessor?
Generic preprocessor: How to use for any kind of file?
What's the macro to distinguish ifort from other fortran compilers?

Categories

HOME
phpword
kentico
codenvy
jmeter
phpmyadmin
interface
opendj
soa
cakephp-3.4
zip
echo
missing-data
gluon-mobile
discrete-mathematics
rcloud
enthought
heap
startup
cmdb
angular-dart
selenium-ide
comsol
qsqlquery
scsi
aws-sdk
jersey-1.0
git-extensions
swagger-2.0
enterprise-miner
topology
google-maps-autocomplete
visual-glitch
displaytag
roundup
data.stackexchange.com
nstimer
oculus
inversion-of-control
angular-translate
sinch
subclass
quantlib
bioinformatics
rselenium
biztalk-rule-engine
hunspell
pdw-file-browser
imx6
lubridate
numpy-broadcasting
apiary.io
protein-database
mongoose-schema
chef-solo
stringtokenizer
datacachefactory
movilizer
congestion-control
x++
plan-9
textsum
opennms
dbcontext
dtsearch
twilio-click-to-call
datamaps
manova
singleinstance
compiler-design
flac
trigger.io
cmp
sandcastle
angular2-testing
builtins
iotivity
neon
qiime
omniauth-facebook
spreadjs
aurora
project-organization
createprocess
nio2
grass
concept-insights
assertion
themoviedb-api
dllimport
lazybones
forwarding
google-books
rdflib
liquid-layout
ms-access-2000
uvc
rapidsvn
post-commit-hook
iso-prolog
matrix-inverse
memset
rfc5322
mt4j
jammer
script#
joomla-template
veracity
database-create
fsevents
google-ajax-api
uipagecontrol
http-daemon
globals
drupal-fivestar
dot-emacs
openfaces
sifr
mathematical-notation
soappy
blind

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App