CMake generator expression, differentiate C / C++ code

后端 未结 3 1275
情书的邮戳
情书的邮戳 2020-12-30 03:32

I would like to add -std=c++11 to my

add_compile_options(\"-std=c++11\")

However, this also adds them to compilation of C

3条回答
  •  庸人自扰
    2020-12-30 04:06

    You can use LINKER_LANGUAGE target property to add flag only to C++ targets*:

    add_compile_options(
        "$<$,CXX>:-std=c++11>"
    )
    

    *Note that this will not work for targets with mixed C/C++ sources

    CMAKE_CXX_FLAGS should work fine too:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    

    Probably you need to add them to cache if it set before project command (e.g. in toolchain):

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" CACHE STRING "" FORCE)
    

提交回复
热议问题