How to define a C++ preprocessor macro through the command line with CMake?

后端 未结 4 621
野性不改
野性不改 2020-12-01 05:25

I try to set a preprocessor macro in the command line of CMake. I\'ve tried:

set generator=\"Visual Studio 8 2005\"
set params=-D MY_MACRO=1
cmake.exe -G %ge         


        
4条回答
  •  Happy的楠姐
    2020-12-01 06:05

    A good alternative would be to define a cmake option:

    OPTION(DEFINE_MACRO "Option description" ON) # Enabled by default
    

    Followed by a condition:

    IF(DEFINE_MACRO)
        ADD_DEFINITIONS(-DMACRO)
    ENDIF(DEFINE_MACRO)
    

    Then you can turn that option ON/OFF via command line with cmake using the -D flag. Example:

    cmake -DDEFINE_MACRO=OFF ..
    

    To make sure the compiler is receiving the definition right, you can call make in verbose mode and check for the macro being defined or not:

    make VERBOSE=1
    

    This is a good solution also because make will recompile your code when any of cmake options changes.

提交回复
热议问题