Is there a way to partially pre-process a C or C++ source file? By "partially preprocess" I mean expanding some but not all of the #include directives. For example, I would like to expand #includes pointing to my project headers, but not #includes pointing to other libraries' headers.
I tried to do this by running gcc -E
with only the -I
flags for my project headers and not the -I
flags for the libraries, but that doesn't work because gcc gives an error when it encounters an #include it cannot expand.
EDIT: I do not really care about the preprocessor's behaviour with respect to macro expansion.
In the general case, a partial header expansion is meaningless. Consider the following example:
The
#include
which you don't want to expand, you can replace with something like$$$include
(in short, which cannot be understood by pre-processor). Preferably first you copy original files into the temporary files and then rungcc -E <filename>;
. Once you are done, again replace to the original source files.The only concern here is that you have to go and edit your source files at least once. But that may not be a big deal as you can use the facility provided by your text editor.
The C preprocessor isn't smart enough to do this on its own. If you're only interested in
#include
, you should just roll your own tool (in, say, Perl) to process the source files, expanding#include
lines that interest you and ignoring the rest.This script prefixes uninteresting header lines with
// Ignored
:Now you can do:
And if you want to get really fancy:
Now you can do:
Use
-nostdinc
for gcc (or cpp).How about this?:
Then, to compile everything,
gcc -DPART_2_INCLUDES -DPART_2_INCLUDES ...
Or, since it seems like usually everything should be included by default and not including some items is the special case, reverse the sense of the tests: