Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partially preprocess a C or C++ source file?

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.

like image 597
HighCommander4 Avatar asked Jun 26 '11 05:06

HighCommander4


People also ask

Which are the C preprocessor?

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.

What is C preprocessor give an example of C preprocessor directive?

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.

What are pre processor in C explain each of them with examples?

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.

What is a preprocessor statement in C?

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.


1 Answers

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
like image 182
Jon Purdy Avatar answered Sep 23 '22 14:09

Jon Purdy