Jon Chesterfield
Jon Chesterfield

Reputation: 2341

Detect when only preprocessing, i.e. gcc -E

I currently invoke clang or gcc as

cc -E -DPREPROCESSING ...

when debugging macros.

It has occurred to me that the define is redundant. Is there an expression I could write in the source to detect when the compiler will stop after preprocessing, and so drop this definition from my build scripts?

#if magic
#define PREPROCESSING
#ending

A look at the docs suggests not, but with luck I'm missing something.

Upvotes: 0

Views: 185

Answers (1)

rici
rici

Reputation: 241671

Whatever solution you come up with is going to be compiler-specific, since the C standard does not have anything to say about separate preprocessing.

In gcc, you could implement the magic by adding a custom spec file:

%rename cpp old_cpp
*cpp:
%{E:-DPREPROCESSING} %(old_cpp)

You would need to tell gcc to use this spec file (-specs=/path/to/specfile), unless you compiled your own gcc with the above definition added to the built-in cpp spec. If you are using a Makefile, you could add the -specs option above to your CFLAGS.

(I should add that I don't think this is a particularly good idea. But it is possible.)

Upvotes: 1

Related Questions