我需要将目标(仅标头库)/std:c++14的标志替换为. CMake 还不支持直接在 VS 中设置 C++17 标志(请参阅如何使用 CMake 在 VS2017 中启用 /std:c++17),因此我需要手动替换它。INTERFACE/std:c++17
但是,get_target_property(my_compile_flags mylib COMPILE_OPTIONS)要检索当前设置的标志列表,然后将 /std:c++14 替换为 /std:c++17 不起作用:
INTERFACE_LIBRARY 目标只能具有白名单属性。不允许使用属性“COMPILE_OPTIONS”。
但是,您可以通过target_compile_features(...)例如手动设置它们target_compile_options(mylib INTERFACE /std:c++17)。但后一个命令添加了标志,但没有删除/std:c++14.
该怎么做呢?
对于接口库,您需要更改INTERFACE_COMPILE_DEFINITIONS而不是COMPILE_DEFINITIONS(参见 参考资料add_library(INTERFACE))。
这是我用 VS2017 测试过的完整示例(使用/std:c++latest尚未支持的内容/std:c++17可能会被 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4040 次 |
| 最近记录: |