Preprocessing C / C ++ Metaprograms

We have a large C ++ project and we would like to send only the code requested by the client, thereby deleting all the code that is not needed. That is, if we have some metaprograms like:

/** File: calc.c */
#ifdef ENABLE_SOME_ADVANCED_FEATURE
/** Advanced calculations */
void AdvancedCalc(int a, int b) {
   // ...
}
#else
/** Basic calculations */
void BasicCalc(int a, int b) {
   // ...
}
#endif

I would like some kind of script to pre-process the C ++ metaprograms, so if I wanted only basic calculations after running the script, the file would look like this:

/** File: calc.min.c */
/** Basic calculations */
void BasicCalc(int a, int b) {
   // ...
}

Thus, all the code that we did not want to send was deleted.

I'm sure there should be something like this.

Update:

I think, How to get rid of ifdef in a big c project is the solution I was looking for.

+3
source share
5 answers

Clang or g ++

g++ -E source.cpp

-E ; . , .

, , .

Visual Studio

stdout

cl /E source.cpp

cl /P source.cpp

, (ignore-header.pl)

#!/usr/bin/perl

use warnings;
use strict;

while (my $line = <>) {

    if ($line !~ /^#include/) {
        print $line
    }
}

cat source.cpp | perl ignore-headers.pl | g++ -E -nostdinc -nostdin++ -x ++ -

g++

Visual Stuido

GCC

+1

GCC

gcc -E

GCC

0

gcc -E filename GCC

0

:

  • gcc -E, , -H, .

    2. -MD, .d. .d script , , , , .

  • , debuginfo, /usr/src/debug ( , RPM).

0

script, :

  • , , " ", //@TAKE_ME_BACK@ ;
  • run cpp -C (cpp -C , .c). : ( -C )!
  • uncomment - , ,

#ifdef, , awk (pattern1, pattern2) .

, , - :

sed 's|^#include|//@TAKE_ME_BACK@#include|' $file > $file.tmp
cpp -C $file.tmp > $file.tmp2
sed -e '/^#/d' -e 's|//@TAKE_ME_BACK@||' < $file.tmp2 > $file

( , #, sed).

1: I, , : :)

Note2: it works, but is not checked - just a proposal that needs to be developed / customized.

0
source

All Articles