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 }
#undef directive (C/C++) Removes (undefines) a name previously created with #define .
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.
4. Which gcc option undefines a preprocessor macro? Explanation: None.
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.
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.
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With