当前位置:网站首页>CMake教程Step3(添加库的使用要求)
CMake教程Step3(添加库的使用要求)
2022-07-05 16:36:00 【开始沸腾了】
CMake官方文档
参考官方cmake3.24教程翻译
https://cmake.org/cmake/help/v3.24/guide/tutorial/index.html
https://gitlab.kitware.com/cmake/cmake/-/tree/master/Help/guide/tutorial
step3
https://cmake.org/cmake/help/v3.24/guide/tutorial/Adding%20Usage%20Requirements%20for%20a%20Library.html
我的仓库 :
https://github.com/FRBoiling/cmake-tutorial.git
库的使用要求允许更好地控制库或可执行文件的链接和包含行,同时还可以更好地控制 CMake 中目标的传递属性。
主要命令
- target_compile_definitions()
- target_compile_options()
- target_include_directories()
- target_link_libraries()
重构项目
让我们基于CMake教程Step2中的项目进行重构(Step3),以满足现代的CMake中的使用要求 。
我们首先声明,库MathFunctions的任何链接,除MathFunctions本身以外,都需要包含当前源目录。
因此,这可能成为接口使用要求。
记住INTERFACE意味着消费者需要而生产者不需要的东西。在MathFunctions/CMakeLists.txt更改后如下:
add_library(MathFunctions mysqrt.cxx)
# state that anybody linking to us needs to include the current source dir to find MathFunctions.h, while we don't.
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
现在我们已经指定了MathFunctions的使用要求.
安全地从顶级CMakeLists.txt中删除EXTRA_INCLUDES变量的使用,找到如下位置进行更改:
...
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
# list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
...
# add the binary tree to the search path for include files so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
# ${EXTRA_INCLUDES}
)
一旦完成,运行cmake可执行文件或cmake-gui来配置项目,然后使用您选择的构建工具或使用cmake——build构建它。从构建目录。
重构后,完整的顶级目录CMakeLists.txt内容如下
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(Tutorial VERSION 1.0)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# should we use our own math functions
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# configure a header file to pass some of the CMake settings
# to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
# add the MathFunctions library
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
endif()
# add the executable
add_executable(Tutorial tutorial.cxx)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
测试
现在让我们更新USE_MYMATH的值。
最简单的方法是在终端中使用cmake-gui或ccmake。
你也可以通过从命令行修改这个选项,如下
cmake …/Step3 -DUSE_MYMATH=ON
或者
cmake …/Step3 -DUSE_MYMATH=OFF
cd Step3_build
cmake ../Step3 -DUSE_MYMATH=ON
cmake --build .
边栏推荐
- Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
- 7.Scala类
- 时间戳strtotime前一天或后一天的日期
- DenseNet
- Etcd 构建高可用Etcd集群
- 【性能测试】全链路压测
- [Jianzhi offer] 62 The last remaining number in the circle
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- easyNmon使用汇总
- Browser rendering principle and rearrangement and redrawing
猜你喜欢

If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats

Detailed explanation of use scenarios and functions of polar coordinate sector diagram

Practical example of propeller easydl: automatic scratch recognition of industrial parts

【机器人坐标系第一讲】

Jarvis OJ Flag

PHP人才招聘系统开发 源代码 招聘网站源码二次开发

Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)

Error in composer installation: no composer lock file present.

NPM installation

ECU introduction
随机推荐
Precision epidemic prevention has a "sharp weapon" | smart core helps digital sentinels escort the resumption of the city
Jarvis OJ Telnet Protocol
easyNmon使用汇总
Jarvis OJ 远程登录协议
PHP人才招聘系统开发 源代码 招聘网站源码二次开发
Learnopongl notes (I)
Jarvis OJ Flag
激动人心!2022开放原子全球开源峰会报名火热开启!
dried food! Semi supervised pre training dialogue model space
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
[brush questions] effective Sudoku
Embedded -arm (bare board development) -1
【性能测试】jmeter+Grafana+influxdb部署实战
Jarvis OJ shell traffic analysis
【剑指 Offer】66. 构建乘积数组
【剑指 Offer】61. 扑克牌中的顺子
Little knowledge about C language (array and string)
调查显示传统数据安全工具面对勒索软件攻击的失败率高达 60%
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
【7.7直播预告】《SaaS云原生应用典型架构》大咖讲师教你轻松构建云原生SaaS化应用,难题一一击破,更有华为周边好礼等你领!