[英]cmake add_library with minimum cpp standard
library needs atleast cpp14 but if cpp17 is available, it unlocks more features.库至少需要 cpp14,但如果 cpp17 可用,它会解锁更多功能。 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:我想要的是:
But what happens is that awesomelib is compiled (only once) according to the c++14 std.但是发生的情况是,awesomelib 是根据 c++14 标准编译的(仅一次)。
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.
我正在寻找图书馆如何宣传其最低要求。 ie it needs at least c++14 standard and depending on the user, it has to be compiled with whatever latest version the user has.
即它至少需要 c++14 标准,并且取决于用户,它必须使用用户拥有的任何最新版本进行编译。
I added a few changes to your CMkaeLists.txt file and it seems to work now我对您的 CMkaeLists.txt 文件进行了一些更改,现在似乎可以使用了
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.