63

How do I at compile time undefine a compiler macro using gcc. I tried some compile args to gcc like -D but I can't get to see the "not defined" message.

Thanks

#include <iostream>

#define MYDEF


int main(){
#ifdef MYDEF
  std::cout<<"defined\n";
#else
  std::cout<<"not defined\n";
#endif

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
monkeyking
  • 6,670
  • 24
  • 61
  • 81

6 Answers6

67

You can use the -U option with gcc, but it won't undefine a macro defined in your source code. As far as I know, there's no way to do that.

dcp
  • 54,410
  • 22
  • 144
  • 164
  • 6
    Yeah, `-U` only works with macros also defined on the command line. You can't undefine a macro defined in code from the CL. – Marc W Dec 30 '09 at 02:54
  • @MarcW The requirement would not even make any sense, because the (un)definitions on the command line are understood as preceding the program text. So it's like wanting an `#undef` at line 16 to neutralize a macro at line 42. – Kaz Jun 17 '21 at 21:09
  • 2
    @MarcW Also, `-U` works not only with macros defined on the command line, but on predefined macros. This is very useful. I'm dealing with a case compiling code on Termux where the builtin definition of `__ANDROID_API__` as `24` is failing to reveal functions that are in fact declared in headers and available for linkage. To fix this, I'm putting a `-U` for this symbol, followed by a `-D` to a higher value. Without the `-U`, you get diagnostics that the command line is redefining a macro differently. – Kaz Jun 17 '21 at 21:14
21

You should wrap the MYDEF definition in a preprocessor macro, the presence of which (defined on the command line) would then prevent MYDEF from being defined. A bit convoluted to be sure but you can then control the build in the way you want from the command line (or Makefile). Example:

#ifndef DONT_DEFINE_MYDEF
#define MYDEF
#endif

Then from the command line when you don't want MYDEF:

gcc -DDONT_DEFINE_MYDEF ...

par
  • 17,361
  • 4
  • 65
  • 80
13

http://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Preprocessor-Options.html#Preprocessor-Options

The -U options seemed like what you could have needed... but then again you can't override a definition contained in your source code without resorting to more preprocessor directives.

jldupont
  • 93,734
  • 56
  • 203
  • 318
4

You can resort to filtering source code and give this back to gcc for compilation, like this pseudo code:

grep -v "define MYDEF" yourFile.c | gcc -o yourFile.o -xc -

Hope it helps.

xryl669
  • 3,376
  • 24
  • 47
1

The code use case is not right. As I see, you have hard coded #define in the file. If compiler initially assumes MYDEF undefined, it will define it once it start processing the file.

You should remove the line #define MYDEF. And I hope your test case will work, if you pass MYDEF to -D and -U.

John_West
  • 2,239
  • 4
  • 24
  • 44
0

Here is one possibility that doesn't completely cover your use case but which I found to be helpful in my case.

If your MYDEF were #defined in a separate header file #included from the .c file you could force the definition of the #include guard macro with the -D option (thus preventing the MYDEF #definition) then either actively #define (still with the -D option) MYDEF to something else or just leave it undefined.

It is clear that anything else defined in the header file would also be missing but this was for me a solution to forcedly undefine a macro without changing the third-party code.

Roland Sarrazin
  • 1,203
  • 11
  • 29
  • Please note my use case: I'm not doing this for production code - there you should properly have the third-party code cleaned - but for unit tests to improve the branch coverage by suppressing third-party code I cannot influence but which is taken account by the code coverage tool as these are macros. Hence the tweak. – Roland Sarrazin Jun 16 '15 at 12:51