robotology/how-to-export-cpp-library

Avoid setting CMAKE_CXX_STANDARD explicitly in libraries

traversaro opened this issue · 1 comments

In abseil/abseil-cpp#259 (comment) , a good point is made against setting CMAKE_CXX_STANDARD explicitly in libraries, in favor of just using target_compile_features(mylib PUBLIC cxx_std_11) to specify the minimum required standard language to compile.
We never set the CMAKE_CXX_STANDARD in this tutorial, but as the use of setting CMAKE_CXX_STANDARD is widespread in robotology libraries, it would be good to have one point where we discuss this. Requiring CMake 3.10 would be a good occasion to switch to use target_compile_features(mylib PUBLIC cxx_std_11).

While I'm in favour of suggesting this, I think there might be a reason for having it in some cases, for example, if your code uses some c++ deprecated features, removed in c++17 (e.g. throw(typeid) or register), your code will not compile if you set target_compile_features(mylib PUBLIC cxx_std_11) but CMake decides to use C++17.

I'm not 100% sure if this is true, and the documentation does not specify it, but if I remember correctly, I added it in YARP when GCC switched to have c++14 enabled by default, and YARP was being compiled without the -std=c++11 flag (therefore equivalent to -std=c++14), setting the variable restored the build flag.

Requiring CMake 3.10 would be a good occasion to switch to use target_compile_features(mylib PUBLIC cxx_std_11).

Looking forward to that day!