0

I have to preprocess a header, during this I want to only process the header for #ifdef, #if defined sections, other sections like Macro expansion and #include sections should not be expanded or checked. how to get this? I need to pass this output file to another software like cmock to create a mock file and then use it for compilation. but the problem I am facing is if I use gcc -E it generates a file where all #includes are expanded, Macros expanded along with #if defines. any help would be appreciated

for example:

#include <stdint.h>   //i don't want to pre-process this line

#if defined (__MACRO_A__)  //i want to pre-process this line
    void funcA(void);
#else
    void funcB(void);
#endif

I want the output file to have only funcB prototype. I have not defined MACRO_A, so if I preprocess header only funcB is getting included, but all the #includes are getting expanded, which I don't want.

  • `I tried gcc -E option but it is not what I want` Why is it not what you want? The line `#if define __MACRO_A__` is invalid, an error `error: missing binary operator before token "__MACRO_A__"`. It's `define<>` not `define`. – KamilCuk Jul 07 '22 at 07:45
  • Do not define __MACRO_A__ – 0___________ Jul 07 '22 at 07:45
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jul 07 '22 at 07:55
  • `i don't want to pre-process this line` So remove the line. Either manually or with some tool. `sed '/#include/d'` – KamilCuk Jul 07 '22 at 08:08
  • If you do not need to compile the original header, you could write it in a different macro language such as **m4**. – Ian Abbott Jul 07 '22 at 09:00
  • I found a duplicate, containing various more or less ideal solutions, including a flavour of the `grep` answer posted here. – Lundin Jul 07 '22 at 09:25
  • Some Unix distributions include an `unifdef` command that removes preprocessor conditionals. – Eric Postpischil Jul 07 '22 at 10:32
  • I finally used http://coan2.sourceforge.net/index.php – sandeep akkanapragada Aug 03 '22 at 14:40

1 Answers1

0

but all the #includes are getting expanded, which I don't want.

So remove them from the file.

grep -v '#include' inputfile.c | gcc -E -xc -

The regex above can possibly be better.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111