c++cmakebuild-definitionbuild-dependenciesheader-only

How do I force dependents on my INTERFACE library to use C++11 or later?


I'm in the process of splitting a library of mine into a header-only lib and a compiled lib, so, for the first time, I'm trying to use CMake to "build", or rather expose, a header-only library.

Reading this and the CMake documentation, I understand I need to use an INTERFACE library, without sources. But - my headers must be compiled with a C++ language standard version of at least C++11. When I was actually compiling something, I made do with:

set_property(TARGET foo PROPERTY CXX_STANDARD 11)
set_property(TARGET foo PROPERTY CXX_STANDARD_REQUIRED ON)
set_property(TARGET foo PROPERTY CXX_EXTENSIONS OFF)

but that's:

  1. Not exactly what I need for code using the header-only lib - I need to say "at least C++11".
  2. Can't be used on INTERFACE libraries.

I noticed there is no set_property(... INTERFACE). So how am I suppoed to force dependent code to use C++11-or-later?

Edit: I am interested both in answers for constraining an exact C++ version choice in dependents, and for constraining "at least" - in case the latter is problematic/difficult/impossible.


Solution

  • If you have a fairly recent version of cmake, you can try

    target_compile_features(foo INTERFACE cxx_std_11)
    

    This ensures that every library that uses foo, i.e.

    target_link_libraries(bar PRIVATE foo)
    

    will be compiled with C++11. For example if bar uses source file bar.cpp, then bar.cpp will be compiled with std=c++11 by cmake.