Pre-processor

I want to have a C preprocessor that filters some #define statements from the source code without changing anything else.

Why? This should be used to remove a specific client code from sources if the source is transferred to another client.

Does anyone know of an existing solution?

Thanks! Simon

+4
source share
8 answers

Can you use something like awk instead of CPP? Add a few flags to the code surrounding the piece of code you want to remove. For instance:

(...) //BEGIN_REMOVE_THIS_CODE printf("secret code"); //END_REMOVE_THIS_CODE (...) 

then write awk script to remove this code, something like ...

 BEGIN { write=1;} /^\/\/BEGIN_REMOVE_THIS_CODE/ { write=0; next;} /^\/\/END_REMOVE_THIS_CODE/ { write=1; next;} { if(write==1) print $0; } 
+10
source

This is similar to what I asked in Is there a C preprocessor that excludes ifdef blocks based on specific values . The best answer I received was sunifdef , or โ€œSon of unifdefโ€, which worked reliably for me on some overly distorted conditional code (accumulated crud from more than 20 years of development on various platforms with an inadequate theory of how to make compilation specific to platforms).

+2
source

I do not think that a preprocessor is needed for this. If you donโ€™t have nested #ifdef in your code, any regex engine can remove anything between #ifdef CLIENT and #endif (use an undesired match to match the first #endif , not the last).

+1
source

I would put the specific client code in a separate directory, or perhaps part of another project that would need to be checked from the source control.

Put a function call that would be muffled or (I forgot the correct term) loosely coupled so that another function could be put in its place.

+1
source

I recommend using an extra layer of macro languages โ€‹โ€‹to filter code, such as filepp . You can use the syntax for the C preprocessor to express which parts belong to those clients.

 //%ifdef CLIENT_A code for client A //%endif //%ifdef CLIENT_B code for client B //%endif //%if "CLIENT_A" || "CLIENT_B" code for client A and B //%endif 

The prefix '//%' allows you to compile the code without changes. You can run filepp before issuing code to the client.

+1
source

If you use gcc, you can use:

 gcc <insert files here> -E 

The -E option tells gcc to only pre-process sources, rather than compiling them.

Or you can use grep to filter specific files and only let the preprocessor get rid of them.

 grep -r '#define CLIENT_CODE' ./*.h 
0
source

You can also try unifdef , which is much simpler than sunifdef.

0
source

as far as I know ... the preprocessor can be started as a separate step (using the correct compiler options). This way you can do whatever you want with processed code.

-2
source

All Articles