Printing name and value of a macro

前端 未结 11 2560
别跟我提以往
别跟我提以往 2020-12-28 21:46

I have a C program with a lot of optimizations that can be enabled or disabled with #defines. When I run my program, I would like to know what macros have been

11条回答
  •  被撕碎了的回忆
    2020-12-28 22:27

    Writing a MACRO that expands to another MACRO would require the preprocessor to run twice on it.
    That is not done.

    You could 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 define lines on this file as with the other files.
    Call the function sometime at the start.

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

    Hopefully, you have the same set of defines allowed across all your source files.
    Otherwise, it would be prudent to recheck the strategy of your defines.


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

提交回复
热议问题