Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To check whether Macro is defined or not

Tags:

c

macros

How can we check wheather a Macro is defined or not, and if defined then with what value? I tried doing gdb, but we can not see the Macros in GDB, as MACROs are replaced at the time of Precompilation only.

Is there any way in GCC compiler that we can see the precompiled file, which is created by the compiler before creating the object file (*.o) ?

like image 568
CodeCodeCode Avatar asked Aug 14 '12 13:08

CodeCodeCode


2 Answers

You can use -E flag of gcc to get the pre-processed output. This output will contain macros expanded instead of their names. You can find more information here.

like image 180
perreal Avatar answered Oct 11 '22 22:10

perreal


Inside of a C source file, you can use the #ifdef macro to check if a macro is defined.

#include <stdio.h>

#ifdef MY_MACRO
char msg[] = "My macro is defined";
#else
char msg[] = "My macro is NOT defined";
#endif

int main(int argc, char **argv) {
    printf("%s\n", msg);

    return 0;
}
like image 26
ryucl0ud Avatar answered Oct 11 '22 22:10

ryucl0ud