简体   繁体   English

cmake add_library 具有最低 cpp 标准

[英]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:我想要的是:

  1. dummy14 to compile using the C++14 standard dummy14 使用 C++14 标准编译
  2. dummy17 and awesomelib to compile using the C++17 standard dummy17 和 awesomelib 使用 C++17 标准编译

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.

相关问题 CMake add_library包含其他库 - CMake add_library containing other libraries 是否存在CMake add_library的环境变量( <lib> 宾语 <src> )? - Are there environment variables for CMake add_library(<lib> OBJECT <src>)? cmake add_library不初始化静态全局变量 - cmake add_library doesn't initialize static global variable 哪个变量用于CMake的ADD_LIBRARY函数的编译器标志? - Which variable for compiler flags of CMake's ADD_LIBRARY function? 我应该只对原始cpp文件使用add_executable()还是通过add_library()创建一个库? - Should I use only add_executable() with raw cpp files or make a library via add_library()? cmake 错误 [SDK/Util/CMakeLists.txt:132 (add_library) 处的 CMake 错误] - cmake error [CMake Error at SDK/Util/CMakeLists.txt:132 (add_library)] CMake 项目结构:何时使用 add_library/target_link_library 而不是 target_sources? - CMake Project structure: When to use add_library/target_link_library over target_sources? 为什么 CMake 没有与我的自定义库 (add_library/set_property) 链接? - Why is CMake not linking with my custom library (add_library/set_property)? 如何在使用add_library不包括特定目标文件时告诉CMake - How to tell CMake when using add_library to not include a specific object file CMake 3.0 INTERFACE 类型的 add_library 中断 get_target_property - CMake 3.0 add_library of type INTERFACE breaks get_target_property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM