CMake: Set different name for #cmakedefine variable

前端 未结 2 589
忘掉有多难
忘掉有多难 2021-01-28 20:25

I know you can use CMake\'s configure_file to make CMake variables available to your program. For example, I can use

#define ${CMAKE_BUILD_TYPE}
         


        
2条回答
  •  梦如初夏
    2021-01-28 21:13

    This is more a question of your preferred programming style (configuration files vs. compiler definitions).

    In your case I would use add_definitions() or directly modify/append COMPILE_DEFINITIONS directory property (using generator expressions also supports multi-configuration environments):

    set_property(
        DIRECTORY 
        APPEND 
        PROPERTY 
            COMPILE_DEFINITIONS "BUILD_TYPE_$>"
    )
    

    Most simplified Debug/Release Check

    You can also check what compiler definitions CMake does pre-define. Without having to modify/add something to your CMakeLists.txt files you could simply check for NDEBUG definition (set for Release builds across platforms) in you C/C++ code:

    #ifdef NDEBUG
    ... 
    #endif
    

    References

    • CMake: How to pass preprocessor macros
    • How to check if a CMake build directory build type is Debug or Release?
    • What predefined macro can be used to detect debug build with clang?

提交回复
热议问题