1

I have a C file with some #include directives as well as #if, #ifdef, etc. I do NOT want #include directives to be expanded with the contents of the included files. I want #if directives and friends to be processed normally. How to do that with GCC?

I first tried -fdirectives-only option, but it processes both #include and #if directives.

Say I have a C file like this

#include <stdio.h>
#if FOO
int foo() {}
#else
int bar() {}
#endif

I want to get a preprocessed file

#include <stdio.h>
int foo() {}

or

#include <stdio.h>
int bar() {}

depending on you give -DFOO=1 or -DFOO=0.

However, with

gcc -E -DFOO=1 -fdirectives-only a.c 

(or with any command line I tried, for that matter), I always get #include <stdio.h> part expanded into > 1000 lines.

Note that my goal here is to generate multiple source files from a single file for humans to read, for which completely expanding #include and macros is NOT what I want.

  • Why not wrap the `#include` in a `#IF`? – daShier Oct 12 '19 at 03:51
  • Once upon a long time ago, questions like this were readily allowed on SO, and I asked about what you want, and the answers are still valid. I mainly use `sunifdef`; `coan` is also available. Both work. These days, questions like this are not generally allowed on SO (and the dual tagging with C and C++ is not usually appropriate either — things have changed a lot in the last decade!). – Jonathan Leffler Oct 12 '19 at 05:38
  • The problem with your intention is that the macro controlling conditional compilation may be defined in an included header, and may itself be conditionally defined; if you don't pre-process the header the semantics may be entirely different. It would be possible to have a pre-processor process but not expand the content of headers, but that is not the standard C preprocessor. JL's suggestions may fit the bill. – Clifford Oct 12 '19 at 07:03
  • Most modern code editors and IDE's will "grey-out" unused code blocks by dynamically pre-processing as you type, and support "code-folding" so you can hide the unused block. That might meet your requirements, and is probably a far more useful/powerful solution if it is presentation rather then code generation that is your concern. – Clifford Oct 12 '19 at 07:07

0 Answers0