如何在不同的CMakeList.txt之间共享变量?
How to share variables between different CMakeList.txt?
我有一个有多个CMakeList.txt
的项目,一个用于代码,一个用于单元测试,另外两个用于库,我想分享一些CMakeList.txt
行以避免重复,例如:
cmake_minimum_required(VERSION 3.0)
或set(CMAKE_CXX_STANDARD 17)
我可以使用类似include("MyProject/CMakeConfig.txt")
的东西吗?
通常,您有一个 CMake 列表文件位于项目的顶层,另一个用作子目录。由于项目需要一次cmake_minimum_required
和project
,因此您应该可以不写任何内容。
下面是一个结构示例:
顶级./CMakeLists.txt
:
cmake_minimum_required(VERSION 3.0)
project(my-project CXX)
# set global property for all this file and subdirectories
set(CMAKE_CXX_STANDARD 17)
# needs to be at the top level
enable_testing()
add_subdirectory(src)
add_subdirectory(test)
在src/CMakeLists.txt
:
add_executable(your-exec file1.cpp file2.cpp file3.cpp)
add_library(your-lib file4.cpp file5.cpp file6.cpp)
find_package(liba REQUIRED)
target_link_libraries(your-exec PUBLIC liba::liba)
target_include_directory(...)
# ...
在test/CMakeLists.txt
:
find_package(Catch2 REQUIRED)
add_executable(test1 test1.cpp)
# link into you lib and a test framework
target_link_libraries(test1 PRIVATE your-lib Catch2::Catch2)
add_test(NAME test1 COMMAND test1)
所有添加的子目录都将继承项目的基本属性,例如最低 CMake 版本、启用的语言和显式设置的基于目录的属性,例如CMAKE_CXX_STANDARD
。
综上所述,添加 C++17 作为使用项目的要求总是一个好主意:
# All users of your-lib need C++17
target_compile_features(your-lib PUBLIC cxx_std_17)
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 如何将 txt 文件中的行分隔为组件C++
- 在cuda线程之间共享大量常量数据
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 一种在C++中读取TXT配置文件的简单方法
- 如何在不同的CMakeList.txt之间共享变量?
- 如何从txt文件c++中提取括号之间的数字
- 如何从.txt文件中读取多个2D数组,而无需每个之间的新线
- C 中的.dat和.txt之间的差异
- 矢量元素和 txt 文件行之间的字符串比较不起作用