-->

CMake: Replace compile flags of an INTERFACE targe

2020-08-01 05:56发布

问题:

I need to replace the /std:c++14 flag of an INTERFACE target (header-only library) with /std:c++17. CMake doesn't support setting C++17 flags in VS directly yet (see How to enable /std:c++17 in VS2017 with CMake) so I need to manually replace it.

However get_target_property(my_compile_flags mylib COMPILE_OPTIONS) to retrieve the list of currently set flags to then subsequently replace /std:c++14 with /std:c++17 doesn't work:

INTERFACE_LIBRARY targets may only have whitelisted properties. The property "COMPILE_OPTIONS" is not allowed.

You can set them however via target_compile_features(...) and manually via e.g. target_compile_options(mylib INTERFACE /std:c++17). But the latter command adds the flag, without removing /std:c++14.

How to go about that?

回答1:

For an interface library you need to change INTERFACE_COMPILE_DEFINITIONS instead of COMPILE_DEFINITIONS (see add_library(INTERFACE)).

Here is a full example I've tested with VS2017 (using /std:c++latest since not yet supported /std:c++17 may be ignored/removed by CMake):

cmake_minimum_required(VERSION 3.8)

project(InterfaceLibCppStd)

include(CheckCXXCompilerFlag)

file(WRITE "mylib/Definitions.h" [=[ 
    #define HELLO_TEXT "Hello Interface Lib"
]=])
add_library(mylib INTERFACE)

target_include_directories(mylib INTERFACE "mylib")
target_compile_options(mylib INTERFACE "/std:c++14")

file(WRITE "main.cpp" [=[
    #include "Definitions.h"
    #include <iostream>

    int main()
    {
        std::cout << HELLO_TEXT << std::endl;
    }
]=])
add_executable(myexe "main.cpp")

if (MSVC_VERSION GREATER_EQUAL "1900")
    CHECK_CXX_COMPILER_FLAG("/std:c++latest" _cpp_latest_flag_supported)
    if (_cpp_latest_flag_supported)
        get_target_property(_opt_old mylib INTERFACE_COMPILE_OPTIONS)
        string(REPLACE "14" "latest" _opt_new "${_opt_old}")
        set_target_properties(mylib PROPERTIES INTERFACE_COMPILE_OPTIONS "${_opt_new}")
   endif()
endif()

target_link_libraries(myexe PUBLIC mylib)


标签: c++ cmake c++17