1

library needs atleast cpp14 but if cpp17 is available, it unlocks more features. I tried the below.

cmake_minimum_required(VERSION 3.10)
project(dummy)

add_library(awesomelib STATIC awesomelib.cpp awesomelib.h)
target_compile_features(awesomelib INTERFACE cxx_std_14)

add_executable(dummy14 main.cpp)
target_link_libraries(dummy14 awesomelib)
target_compile_features(dummy14 PRIVATE cxx_std_14)

add_executable(dummy17 main.cpp)
target_link_libraries(dummy17 awesomelib)
target_compile_features(dummy17 PRIVATE cxx_std_17)

What I want is:

  1. dummy14 to compile using the C++14 standard
  2. dummy17 and awesomelib to compile using the C++17 standard

But what happens is that awesomelib is compiled (only once) according to the c++14 std.


Update

I have shown the executables in the same file for simplicity. In real setup, the library would be in a separate project/repository and the users will be in a different project. I am looking for how the library can advertise its minimum requirements. i.e it needs at least c++14 standard and depending on the user, it has to be compiled with whatever latest version the user has.

1 Answer 1

1

I added a few changes to your CMkaeLists.txt file and it seems to work now

cmake_minimum_required(VERSION 3.10)
project(dummy)
set (CMAKE_CXX_STANDARD 17)

add_library(awesomelib STATIC awesomelib.cpp awesomelib.h)
target_compile_features(awesomelib INTERFACE)

add_executable(dummy17 main.cpp)
target_link_libraries(dummy17 awesomelib)
target_compile_features(dummy17 PRIVATE cxx_std_17)

set (CMAKE_CXX_STANDARD 14)
add_executable(dummy14 main.cpp)
target_link_libraries(dummy14 awesomelib)
target_compile_features(dummy14 PRIVATE cxx_std_14)

enter image description here enter image description here enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.