CMAKE - setting compile flags for libraries

后端 未结 1 1564
别那么骄傲
别那么骄傲 2020-12-18 12:17

I have a CMakeLists.txt script to compile my libraries. The problem is I cannot set the compile flags for the libraries.

I\'ve tried

SET(CMAKE_CXX_F         


        
相关标签:
1条回答
  • 2020-12-18 12:40

    Those are globally cached variables you are trying overwrite in your first approach.

    Changing those compiler/linker options locally you need config-specific target properties or generator expressions. So taking your second approach and How can I set specific compiler flags for a specific target in a specific build configuration using CMake? I would get:

    target_compile_options(
        MyLib PRIVATE 
        /W3 /nologo /EHsc
        "$<$<CONFIG:Debug>:/MTd;/Od;/Ob0;/Zi;/RTC1;/DDEBUG;/D_DEBUG>"
        "$<$<CONFIG:Release>:/MT;/O2;/Ob2;/DNDEBUG>"
    )
    

    or for CMake versions <= 2.8.12 (see also policy CMP0043):

    set_target_properties(
        MyLib PROPERTIES 
            COMPILE_FLAGS 
                "/W4 /nologo /EHsc"
            COMPILE_FLAGS_DEBUG
                "/MTd /Od /Ob0 /Zi /RTC1"
            COMPILE_FLAGS_RELEASE
                "/MT /O2 /Ob2"
            COMPILE_DEFINITIONS_DEBUG
                "DEBUG;_DEBUG"
            COMPILE_DEFINITIONS_RELEASE
                "NDEBUG"
    )
    

    I personally like the "OLD" way of doing it better, because it replaces the default options in Visual Studio's project properties. Whatever is passed via target_compile_options() will end up in Configuration Properties / C/C++ / Command Line / Additional Options.

    Some background information why your first approach didn't work:

    CMake's generator takes whatever is set for CMAKE_<LANG>_FLAGS at the end of any CMakeLists.txt file and applies it to all library and executable targets in the same CMakeLists.txt file as the default compiler options when generating the build environment.

    If you set the linker variables for you main CMakeLists.txt targets in subdirectories CMakeLists.txt it won't help (wrong scope). If I take your above code into the main CMakeLists.txt it does work and I get the following warnings (because you used compiler options for the linker):

    1>LINK : warning LNK4044: unrecognized option '/W3'; ignored
    1>LINK : warning LNK4044: unrecognized option '/EHsc'; ignored
    1>LINK : warning LNK4044: unrecognized option '/MTd'; ignored
    1>LINK : warning LNK4044: unrecognized option '/Od'; ignored
    1>LINK : warning LNK4044: unrecognized option '/Ob0'; ignored
    1>LINK : warning LNK4044: unrecognized option '/Zi'; ignored
    1>LINK : warning LNK4044: unrecognized option '/RTC1'; ignored
    1>LINK : warning LNK4044: unrecognized option '/DDEBUG'; ignored
    1>LINK : warning LNK4044: unrecognized option '/D_DEBUG'; ignored
    

    I'm now hiding the cached variables of the same name set by CMake during the compiler detection. For more details see:

    • What's the CMake syntax to set and use variables?
    • CMake: In which order are files parsed (cache, toolchain, etc.)?
    0 讨论(0)
提交回复
热议问题