0

I have a source tree for a program I am working on which is written in mixed C/C++ code. For debugging purposes, I would like to be able to run a command line tool like unifdef on the entire tree (recursively) to remove a certain set of #ifdef/#endif macros from all source files.

I was wondering if there was any specific way I could go about doing this in an efficient way. Any help would be appriciated, thank you.

beeselmane
  • 1,111
  • 8
  • 26
  • You need to do this in C or C++, or you need to write a shell script that does this? – Barmar May 07 '17 at 16:41
  • 1
    Use the `find` command with the `-exec` option to execute a command on all the files in a souce tree. – Barmar May 07 '17 at 16:42
  • 1
    There is no "mixed C/C++ code". A single source file can only be either C or C++. A c++ compiler cannot compile C code. – too honest for this site May 07 '17 at 16:43
  • @Olaf I think he means that he has both C and C++ source files in his project. – Barmar May 07 '17 at 16:44
  • 1
    Unclear. The code inside an `#ifdef` directive will be compiled if the symbol is defined. Why can't you just define (or undefine) the symbol from makefile/build script ? – kebs May 07 '17 at 16:47
  • @Barmar: Who knows? There are a lot of people thinking C coding style **is** C. – too honest for this site May 07 '17 at 16:48
  • @Olaf "A c++ compiler cannot compile C code." Eh??? Did you mean the other way around? – DYZ May 07 '17 at 16:54
  • @DYZ: No. They are different languages! Read the standards. **Identical syntax/grammar does not imply identical semantics**. Read my comments **carefully** again! – too honest for this site May 07 '17 at 16:56
  • @Olaf Surely I can compile almost any C code with a C++ compiler. There are C statements that have different semantics from their C++ lookalikes, but your statement is too strong to be true. – DYZ May 07 '17 at 17:02
  • They have a nearly identical preprocessor. I have both C and C++ source files which share macros. Of course you cannot compile C++ with a C compiler (although generally the reverse does work properly). I'm not trying to compile anything, just remove unused macros. – beeselmane May 07 '17 at 17:10
  • @DYZ: Try these simple definitions: `static const int i = 10; static int a[i] = { 0 };`. You also want to learn about name-mangling. Why do you think one has to use `extern "C"` in C++ for C function declarations? Just learn both languages well enough. As you seem not to have understood what I wrote: If you use C coding style in C++ does not make your program C compatible! – too honest for this site May 07 '17 at 17:15
  • @Olaf I have multiple source files. There are some files with C++ code and some files which contain C code. In reality, it doesn't even matter because I'm talking about the C preprocessor, not the code itself. (Additionally, the only reason you have to `extern "C"` in C++ is because of symbol mangling, both are still compiled to assembly in effectively the same manner. – beeselmane May 07 '17 at 17:18
  • @DYZ: Better than you do apparently! Note that the two definitions above compile fine in C++, although each one itself is valid for both languages. THe semantics make the difference. – too honest for this site May 07 '17 at 17:18
  • 1
    It seems that you want to remove some `#ifdef`s and the corresponding `#endif` statement, so that the code between the directives now compiles unconditionally. How are you going to treat `#else`s and `#ifndef`s? – M Oehm May 07 '17 at 17:22
  • @M Oehm Not exactly—I want to remove the code between them if it will not be compiled normally and leave the code if it will be compiled normally. I basically just want to evaluate the blocks as the preprocessor would. I want to be able to provide the macros that I want to be defined and then the changes would be computed. – beeselmane May 07 '17 at 17:25
  • 2
    @beeselmane Most compilers have an option to save the output of the preprocessor in a file. So compile your code with that option. – Barmar May 07 '17 at 17:36
  • 2
    See also [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 May 07 '17 at 21:36

1 Answers1

2

I've solved this issue by using the following command:

find . -name '*.c' -o -name '*.h' -o -name '*.cpp' -o -name '*.hpp' -exec unifdef <macro definitions> -o '{} {} ;'

beeselmane
  • 1,111
  • 8
  • 26