3

I am working on a large project using C language, which has a lot of preprocessor macros: #ifdef/#if. The macros are defined in makefile.

In order to get the clean code, I modified the makefile to use "gcc -E". But the gcc preprocessor would expand the included header file as well, which I do not expect.

Is there any method to get rid of the #ifdef/#if without expand the included header files? I searched GCC options but not find an answer yet.

An example:

    #include "a.h"
    #ifdef ABC
    func()
    #else
    func(a)
    #endif
    {
    ...
    }

In makefile, this source is compiled with -DABC, I am looking for a method to change the file to:

    #include "a.h"
    func()
    {
    ...
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jason Ye
  • 31
  • 1
  • 1
    1. I don't see how this is useful. 2. If you *really* need this, then Dhy don't you just make a separate header file with the defines, without any header inclusion, then include this auxiliary header in the other one? –  Apr 24 '13 at 04:44
  • I think the point `large project` makes it difficult – abasu Apr 24 '13 at 05:00
  • Yes, this project has too many files to do this kind of change. – Jason Ye Apr 24 '13 at 05:22
  • 1
    you could put `#ifndef _OMIT_HEADERS_`,`#endif` around the included header files, and then use `gcc -E -D_OMIT_HEADERS_`? – steabert Apr 24 '13 at 05:23
  • The initial purpose of this is to collect some source code metrics of the project, like the cyclomatic complexity. The calculation tools like CCCC can not recognize the preprocess macros. – Jason Ye Apr 24 '13 at 05:25
  • Why can't you use this metric calculator on the `gcc -E` output with all the headers expanded? – Tadeusz A. Kadłubowski Apr 24 '13 at 08:12
  • See [Is there a C pre-processor which eliminates `#ifdef` blocks based on values defined/undefined?](http://stackoverflow.com/questions/525283/is-there-a-c-pre-processor-which-eliminates-ifdef-blocks-based-on-values-define). – Jonathan Leffler Apr 24 '13 at 09:59
  • Thanks! The unifdef is what I am looking for! – Jason Ye Apr 25 '13 at 03:01

1 Answers1

2

If you only want to remove preprocessor conditionals from your code you can use unifdef :

unifdef -DFOO header.h
atenart
  • 319
  • 1
  • 5