Enabling C++11 and later features

#11 · closed · 3 comments

View on GitHub ↗

acgetchell

I've been following the advice given here: https://crascit.com/2015/03/28/enabling-cxx11-in-cmake/ Why in particular do you prefer target_compile_features(PRIVATE cxx_std_YY)? Thanks for the example! I'm doing many of the same things, I haven't worked out how you're doing Doxygen, but I do have integrated tests.

Comments

Lectem

I prefer this because it helps the build system when interfacing with libraries, and it is the kind of properties that can be exported in `*Config.cmake`. The issue with ``` set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` is that it is global, and does not reflect the real needs of your libraries. The good thing with `target_compile_features(PRIVATE cxx_std_YY)` is that if you have lib A that uses ` cxx_std_17` in the interface (PUBLIC/INTERFACE, not PRIVATE) and lib B that uses `cxx_std_11`, CMake will promote lib B usage to c++17. Now if you tried to do the same thing with CMAKE_CXX_STANDARD, you would have something like this: libA.cmake ``` set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Stuff for lib A ``` libB.cmake ``` set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) # Stuff for lib B # link library A ``` Then lib B is not guaranteed to compile, because `CMAKE_CXX_STANDARD 11` and not `CMAKE_CXX_STANDARD 17`. Note that while the `cxx_std_YY` shortcut is good, it is sometimes better to declare exactly what your needs are, as some compilers (or versions) only implement parts of the standard. Hope this helps ! PS: The doxygen part requires CMake 3.9+, hence the `CMAKE_VERSION VERSION_GREATER_EQUAL 3.9`. See https://cmake.org/cmake/help/latest/module/FindDoxygen.html?highlight=doxygen#command:doxygen_add_docs for latest documentation

Lectem

@acgetchell Is the answer ok ? Should I close the issue ?

acgetchell

Great answer, thanks! Last night I thought I had a case where this didn’t work, but it ended up that I wasn’t applying it to that library. Thanks for the guidance!