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.
The C preprocessor is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.
Examples of some preprocessor directives are: #include, #define, #ifndef etc. Remember that the # symbol only provides a path to the preprocessor, and a command such as include is processed by the preprocessor program. For example, #include will include extra code in your program.
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation (Proprocessor direcives are executed before compilation.). It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.
The C preprocessor is a macro preprocessor (allows you to define macros) that transforms your program before it is compiled. These transformations can be the inclusion of header files, macro expansions, etc. All preprocessing directives begin with a # symbol.
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
:
#!/usr/bin/perl
use warnings;
use strict;
my @uninteresting = qw(iostream vector map);
my $uninteresting = join '|', @uninteresting;
while (<>) {
s%(#include <(?:$uninteresting)>)%// Ignored $1%;
print;
}
Now you can do:
cat sourcefile.cpp | perl ignore-meh.pl | g++ -E
And if you want to get really fancy:
#!/usr/bin/perl
use warnings;
use strict;
while (<>) {
s%// Ignored (#include <[^>]+>)%$1%;
print;
}
Now you can do:
cat sourcefile.cpp | perl ignore-meh.pl | g++ -E | perl restore-meh.pl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With