0

I have a simple CMake (3.9.4) project with 3 libraries.

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). 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.

Red XIII
  • 5,771
  • 4
  • 28
  • 29
  • Please include the error you get with the second attempt. – tambre Oct 22 '17 at 16:44
  • 3
    Possible duplicate of [How to enable /std:c++17 in VS2017 with CMake](https://stackoverflow.com/questions/44960715/how-to-enable-stdc17-in-vs2017-with-cmake) – Florian Oct 22 '17 at 17:42

1 Answers1

2

CMake versions higher than 3.10 support MSVC C++ standard switches, but on earlier versions they have no effect.

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.

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. There's an issue tracking this here.

tambre
  • 4,625
  • 4
  • 42
  • 55