C preprocessor: expand macro in a #warning

前端 未结 4 1651
礼貌的吻别
礼貌的吻别 2020-12-12 20:39

I would like to print a macro value (expand the macro) in the #warning directive.

For example, for the code:

#define AAA 17
#warning AAA = ???
         


        
4条回答
  •  自闭症患者
    2020-12-12 21:17

    Many times I have my Makefile generate a local generated.h file that contains the desired definitions.

    generated.h:  Makefile
            echo >generated.h "// WARNING: generated file. Change Makefile instead"
            date >>generated.h '+//   generated on %Y-%m-%d %H:%M:%S'
            echo >>generated.h "#if AAA == AAA_bad"
            echo >>generated.h "#warning \"AAA = $(AAA_bad)\""
            echo >>generated.h "#endif"

    The need for #include "generated.h" is obvious.

    Naturally you can spin any complexity here, but if it gets more then a few lines you may want to put the complexity into a separate script as cluttered Makefiles can be a horrid maintenance issue. With a little Imagination you can have loops generating large numbers of tests from a little input.

    Having the generated.h target depend on Makefile is critical to assure generated.h is remade if the instructions in the target change. If you have a separate generated.sh script that too would be on the dependency list.

    Disclaimer: have not tested for real.

提交回复
热议问题