Partially preprocess a C or C ++ source file?

Is there a way to partially pre-process the original C or C ++ file? By "partially preprocess" I mean extending 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 the headers of other libraries.

I tried to do this by running gcc -E only with the -I flags for my project headers and not the -I flags for libraries, but this does not work because gcc gives an error when #include is encountered it cannot expand.

EDIT : I don't care about the behavior of the preprocessor regarding macro expansion.

+6
source share
5 answers

C preprocessor is not smart enough to do it on its own. If you are only interested in #include , you just need to collapse your own tool (in, say, Perl) to process the source files, expanding the #include lines that interest you and are ignored by the rest.

This script is a prefix for 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 fantasy:

 #!/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 
+5
source

#include , which you do not want to expand, you can replace with something like $$$include (in short, this is impossible to understand using the preliminary processor). It is preferable to first copy the source files to temporary files, and then run gcc -E <filename>; . After you are done, replace the original source files again.

The only problem is that you have to edit the source files at least once. But this may not be a big problem, since you can use the tool provided by your text editor.

+3
source

How about this ?:

 #include <always_include.h> #include <another_always_include.h> #ifdef PART_2_INCLUDES #include <part2.h> #include <part2a.h> #endif #ifdef PART_3_INCLUDES #include <part3.h> #include <part3a.h> #endif ... 

Then, to compile everything, gcc -DPART_2_INCLUDES -DPART_2_INCLUDES ... Or, since it seems that usually everything should be turned on by default and not include some elements - this is a special case, cancel the meaning of the tests:

 #include <always_include.h> #include <another_always_include.h> #ifndef PART_2_INCLUDES_OMITTED #include <part2.h> #include <part2a.h> #endif ... 
+2
source

Use -nostdinc for gcc (or cpp ).

 gcc ... -nostdinc ... 
+1
source

In general, a partial header extension is pointless. Consider the following example:

 #include <limits.h> #if UINT_MAX > 0xffffffff # include "fasthash_64.h" #elif UINT_MAX == 0xffffffff # include "hash.h" #else # error "int too small for hash implementation." #endif 
0
source

Source: https://habr.com/ru/post/891374/


All Articles