CMake:替换接口目标的编译标志
CMake: Replace compile flags of an INTERFACE target
我需要将INTERFACE
目标(仅标头库(的/std:c++14
标志替换为/std:c++17
.CMake 尚不支持直接在 VS 中设置 C++17 标志(请参阅如何使用 CMake 在 VS2017 中启用/std:c++17(,因此我需要手动替换它。
但是,检索当前设置的标志列表,然后随后将/std:c++14 替换为/std:c++17get_target_property(my_compile_flags mylib COMPILE_OPTIONS)
不起作用:
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)
相关文章:
- 即使使用调试编译标志,表达式也是"optimized out"
- 在 CMake 中添加全局编译标志
- 如何在 clang 的自动会议中检查支持编译标志
- CMake:如何在Visual Studio环境中将依赖于模式的编译标志传递给nvcc
- CMake:替换接口目标的编译标志
- GCC - 包含编译标志的宏
- 覆盖单个文件的编译标志
- 通过宏定义编译标志(C++11 和优化)
- 如何强制 Netbeans 只使用 -ggdb 编译标志而不使用它,同时在 Makefile-Debug.mk 中自动发
- 覆盖来自 R 的C++编译标志的系统默认值
- 是否可以在编译时间作为宏上检测GCC编译标志
- 如何将编译标志 -g 添加到 make 文件
- "-Bdynamic"编译标志有什么作用?
- 如何在cpp文件(C++)中设置编译标志
- Qmake 不使用 stdc++11 编译标志
- 如何在 CMake 文件中添加链接器或编译标志
- CMake-删除单个翻译单元的编译标志
- 添加 -fPIC 编译标志后"Recompile with -fPIC"错误仍然存在
- 在不同的编译单元中使用不同的编译标志编译相同的头
- c++链接和编译标志