[英]Enabling support for C++17 in CMake for Visual Studio
I have a simple CMake (3.9.4) project with 3 libraries. 我有一个带有3个库的简单CMake(3.9.4)项目。
Basic library is an INTERFACE one (header only). 基本库是一个接口库(仅标头)。 It's using some features from C++17 that are present in Visual Studio (like constexpr if). 它使用Visual Studio中存在的C ++ 17中的某些功能(如constexpr)。 Obviously I try to set a target property so that it propagated to dependent libraries. 显然,我尝试设置目标属性,以使其传播到依赖库。
I've tried: 我试过了:
target_compile_features(my_lib INTERFACE cxx_std_17)
But nothing changed. 但是什么都没有改变。
Second attempt was: 第二次尝试是:
set_target_properties(my_lib PROPERITES CXX_STANDARD 17)
But I get: 但是我得到:
CMake Error at cpp/CMakeLists.txt:20 (set_target_properties):
INTERFACE_LIBRARY targets may only have whitelisted properties. The
property "CXX_STANDARD" is not allowed.
Finally I ended up with: 最后,我得出以下结论:
target_compile_options(bit INTERFACE /std:c++17)
which works fine. 效果很好。 Is this a correct solution? 这是正确的解决方案吗? Taking a look at all the compile features I believe there should be something better I can do. 看一下所有编译功能,我相信应该可以做得更好。 Also that forces me to wrap the above command in some kind of if(MSVC) ... endif()
shenanigans. 这也迫使我将上述命令包装在某种if(MSVC) ... endif()
恶作剧中。
CMake versions higher than 3.10 support MSVC C++ standard switches , but on earlier versions they have no effect. 高于3.10的CMake版本支持MSVC C ++标准开关 ,但在较早版本上它们无效。
The only portable approach, to ensuring your program is compiled with the correct C++ standard mode on Visual Studio, is to require at least CMake 3.10, set the target property CXX_STANDARD
to your desired value and CXX_STANDARD_REQUIRED
to ON
. 确保在Visual Studio上使用正确的C ++标准模式编译程序的唯一可移植方法是至少要求CMake 3.10,将目标属性CXX_STANDARD
设置为所需的值,并将CXX_STANDARD_REQUIRED
为ON
。
Example usage: 用法示例:
set_property(TARGET my_target PROPERTY CXX_STANDARD 17)
set_property(TARGET my_target PROPERTY CXX_STANDARD_REQUIRED ON)
Note: Currently CXX_STANDARD
documentation for CMake 3.10 incorrectly states that it has no effect on MSVC. 注意:当前CMake 3.10的CXX_STANDARD
文档错误地指出它对MSVC没有影响。 There's an issue tracking this here . 有跟踪这个问题在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.