Print macro name and value

I have a C program with a lot of optimizations that you can enable or disable using #define s. When I run my program, I would like to know which macros were defined at compile time.

So, I'm trying to write a macro function to print the actual value of a macro. Something like that:

 SHOW_DEFINE(X){\ if( IS_DEFINED(X) )\ printf("%s is defined and as the value %d\n", #X, (int)X);\ else\ printf("%s is not defined\n", #X);\ } 

However, I do not know how to make it work, and I suspect that this is impossible, does anyone have an idea on how to do this?

(Note that this must be compiled even if the macro is not defined!)

+7
c c-preprocessor
source share
11 answers

Writing a MACRO that expands to another MACRO will require the preprocessor to execute it twice.
This is not done.

You can write a simple file,

 // File check-defines.c int printCompileTimeDefines() { #ifdef DEF1 printf ("defined : DEF1\n"); #else // DEF1 printf ("undefined: DEF1\n"); #endif // DEF1 // and so on... } 

Use the same compile time options in this file as in other files.
Call the function sometime at the beginning.

If you have #DEFINE lines inside the source file, not a Makefile ,
Move them to another Header file and include this header in all source files,
including check-defines.c .

I hope you have the same set of definitions allowed in all of your source files.
Otherwise, it would be wise to reconsider the strategy of your definitions.


To automate the generation of this function ,
you can use the M4 macro (or even the AWK script actually).
M4 becomes your pre-processor.

+4
source share

As long as you are ready to come to terms with the fact that SOMESTRING = SOMESTRING indicates that SOMESTRING has not been defined (view it because the token has not been redefined!?!), Then the following:

 #include <stdio.h> #define STR(x) #x #define SHOW_DEFINE(x) printf("%s=%s\n", #x, STR(x)) #define CHARLIE -6 #define FRED 1 #define HARRY FRED #define NORBERT ON_HOLIDAY #define WALLY int main() { SHOW_DEFINE(BERT); SHOW_DEFINE(CHARLIE); SHOW_DEFINE(FRED); SHOW_DEFINE(HARRY); SHOW_DEFINE(NORBERT); SHOW_DEFINE(WALLY); return 0; } 

Output:

 BERT=BERT CHARLIE=-6 FRED=1 HARRY=1 NORBERT=ON_HOLIDAY WALLY= 
+25
source share
 #ifdef MYMACRO printf("MYMACRO defined: %d\r\n", MYMACRO); #endif 

I do not think that what you are trying to do is possible. You request information at runtime that has been processed before compilation. The string "MYMACRO" does not mean anything after CPP has expanded it to a value inside your program.

+2
source share

You did not specify the compiler you are using. If it is gcc, gcc -E can help you as it stops after the preprocessing step and prints the result of the preprocess. If you compare the result of gcc -E with the source file, you can get some of what you want.

+2
source share

Why not just test it with a preprocessor?

  #if defined(X) printf("%s is defined and as the value %d\n", #X, (int)X); #else printf("%s is not defined\n", #X); #endif 

You can also paste this into another test so you don't have to print it every time:

  #if define(SHOW_DEFINE) #if defined(X) printf("%s is defined and as the value %d\n", #X, (int)X); #else printf("%s is not defined\n", #X); #endif #endif 
+2
source share

The wave library from boost can also be useful for doing what you want. If your project is large enough, I think it's worth a try. This is a C ++ preprocessor, but they are the same :)

+1
source share

I believe that what you are trying to do is impossible. If you can change the way #define works, I suggest something like this:

 #define ON 1 #define OFF 2 #define OPTIMIZE_FOO ON #define OPTIMIZE_BAR OFF #define SHOW_DEFINE(val)\ if(val == ON) printf(#val" is ON\n");\ else printf(#val" is OFF\n"); 
+1
source share

It is possible that the problem is the "indefinite" part. If you used macros to activate / deactivate parts of your code, you could simply do:

 #define SHOW_DEFINE(X) \ do { \ if (X > 0) \ printf("%s %d\n", #X, (int)X); \ else \ printf("%s not defined\n", #X); \ } while(0) 
+1
source share

Based on Chrisharris answer, I did it. Although my answer is not very pleasant, this is what I wanted.

 #define STR(x) #x #define SHOW_DEFINE(x) printf("%s %s\n", #x, strcmp(STR(x),#x)!=0?"is defined":"is NOT defined") 

Only the error found - this definition should not be #define XXX XXX (with XXX equal to XXX).

0
source share

This question is very close to a macro that prints an expression and evaluates it (using __STRING) . Krisharris’s answer is very close to the answer to the previous question.

0
source share

you can use an integer variable initialized 1. multiply #define by this variable and print the value of the variable.

0
source share

All Articles