Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undefine a define at commandline using gcc

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  } 
like image 897
monkeyking Avatar asked Dec 30 '09 02:12

monkeyking


People also ask

How do I Undefine a #define?

#undef directive (C/C++) Removes (undefines) a name previously created with #define .

How do you redefine #define in C?

The process to redefine a Macro is: Macro must be defined. When, you want to redefine the Macro, first of all, undefined the Macro by using #undef preprocessor directive. And, then define the Macro again by using #define preprocessor directive.

Which gcc option Undefine a preprocessor macro?

4. Which gcc option undefines a preprocessor macro? Explanation: None.

How do I create a preprocessed file in gcc?

You can get a preprocessed file in the MSVC environment by defining the value of the 'Generate Preprocessed File' property on the 'C/C++\Preprocessor' tab as shown in Figure 1. To get a preprocessed file using the GCC compiler you need to add the parameters '-E -o file_name.


2 Answers

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.

like image 73
dcp Avatar answered Sep 29 '22 12:09

dcp


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 ...

like image 32
par Avatar answered Sep 29 '22 13:09

par