当前位置:网站首页>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 .
边栏推荐
- [61dctf]fm
- Use byte stream to read Chinese from file to console display
- Embedded-c Language-2
- Error in composer installation: no composer lock file present.
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- Embedded UC (UNIX System Advanced Programming) -3
- [brush questions] effective Sudoku
- Android privacy sandbox developer preview 3: privacy, security and personalized experience
- 张平安:加快云上数字创新,共建产业智慧生态
- Jarvis OJ Flag
猜你喜欢
国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
阈值同态加密在隐私计算中的应用:解读
Embedded UC (UNIX System Advanced Programming) -3
Learnopongl notes (I)
Bs-xx-042 implementation of personnel management system based on SSM
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Deeply cultivate 5g, and smart core continues to promote 5g applications
composer安装报错:No composer.lock file present.
Deep learning plus
机器学习编译第2讲:张量程序抽象
随机推荐
Data verification before and after JSON to map -- custom UDF
Excuse me, is the redis syntax used in DMS based on the commands of the redis community version of the cloud database
深耕5G,芯讯通持续推动5G应用百花齐放
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
Jarvis OJ shell流量分析
Etcd 构建高可用Etcd集群
Sentinel flow guard
【beanshell】数据写入本地多种方法
American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
【Web攻防】WAF检测技术图谱
【729. 我的日程安排表 I】
China Radio and television officially launched 5g services, and China Mobile quickly launched free services to retain users
[Web attack and Defense] WAF detection technology map
手机开证券账户安全吗?怎么买股票详细步骤
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
Get ready for the pre-season card game MotoGP ignition champions!
Apple has abandoned navigationview and used navigationstack and navigationsplitview to implement swiftui navigation
Solve cmakelist find_ Package cannot find Qt5, ECM cannot be found
ECU简介
It is forbidden to copy content JS code on the website page