How to overwrite a macro definition using compiler options

258 Views Asked by At

I want to redefine a macro, without modifying the header or source files.

For example: #define macro_1 Present -- Is what actually defined in the header file

For one set of execution I need this macro to be same as it is , but for another set of execution I need the macro to be redefined to value 'Absent' This should be done without changing the header file. Am using the ghs compliler.

I tried undef option but its not effectively working

2

There are 2 best solutions below

0
Some programmer dude On

For GCC or Clang, the option to define a macro on the command line is -D. As in -D macro_1=\"Absent\".

You need to use conditional-compilation in the header file though:

#ifndef macro_1
# define macro_1 "Present"
#endif

Otherwise you will get macro redefinition errors.

0
pts On

What you are asking is not possible with most compilers if the code doesn't already contain #ifndef macro_1 or similar.

If it does, then see the answer by.Some programmer dude.