当前位置:网站首页>Cmake tutorial Step3 (requirements for adding libraries)
Cmake tutorial Step3 (requirements for adding libraries)
2022-07-05 17:45:00 【It's beginning to boil】
CMake Official documents
Refer to the official cmake3.24 Course translation
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
My warehouse :
https://github.com/FRBoiling/cmake-tutorial.git
The use requirements of the library allow better control over the links and included lines of the library or executable , At the same time, it can better control CMake The transfer attribute of the target in .
The main command
- target_compile_definitions()
- target_compile_options()
- target_include_directories()
- target_link_libraries()
Refactoring project
Let's base ourselves on CMake course Step2 Refactoring the project in (Step3), To meet modern CMake Requirements for use in .
Let's first state , library MathFunctions Any links to , except MathFunctions Beyond itself , All need to include the current source directory .
therefore , This may become an interface usage requirement .
remember INTERFACE It means what consumers need but producers don't need . stay MathFunctions/CMakeLists.txt The changes are as follows :
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}
)
Now we have designated MathFunctions Requirements for use of .
Safely from the top CMakeLists.txt Delete in EXTRA_INCLUDES Use of variables , Find the following location to change :
...
# 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}
)
Once that is done , function cmake Executable or cmake-gui To configure the project , Then use the build tool of your choice or use cmake——build Build it . From build directory .
After refactoring , Complete top-level directory CMakeLists.txt The contents are as follows
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}"
)
test
Now let's update USE_MYMATH Value .
The simplest way is to use it in the terminal cmake-gui or ccmake.
You can also modify this option from the command line , as follows
cmake …/Step3 -DUSE_MYMATH=ON
perhaps
cmake …/Step3 -DUSE_MYMATH=OFF
cd Step3_build
cmake ../Step3 -DUSE_MYMATH=ON
cmake --build .
边栏推荐
- 外盘黄金哪个平台正规安全,怎么辨别?
- Flask solves the problem of CORS err
- Ant financial's sudden wealth has not yet begun, but the myth of zoom continues!
- Design of electronic clock based on 51 single chip microcomputer
- Is it safe to open an account online? What is the general interest rate of securities financing?
- 漫画:如何实现大整数相乘?(上) 修订版
- 2022年信息系统管理工程师考试大纲
- Matlab reference
- [binary tree] insufficient nodes on the root to leaf path
- WR | Jufeng group of West Lake University revealed the impact of microplastics pollution on the flora and denitrification function of constructed wetlands
猜你喜欢

Compter le temps d'exécution du programme PHP et définir le temps d'exécution maximum de PHP

Kafaka技术第一课

VBA drives SAP GUI to realize office automation (II): judge whether elements exist

Server configuration jupyter environment

Kafaka technology lesson 1

ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲

MySQL之知识点(六)

提高應用程序性能的7個DevOps實踐
MySql 查询符合条件的最新数据行

Machine learning 02: model evaluation
随机推荐
Cartoon: interesting [pirate] question
Seven Devops practices to improve application performance
Troubleshooting - about clip not found Visual Studio
C # realizes crystal report binding data and printing 3-qr code barcode
Flask solves the problem of CORS err
Tips for extracting JSON fields from MySQL
ICML 2022 | Meta提出魯棒的多目標貝葉斯優化方法,有效應對輸入噪聲
企业数字化发展中的六个安全陋习,每一个都很危险!
Design of electronic clock based on 51 single chip microcomputer
33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)
漫画:有趣的海盗问题 (完整版)
外盘黄金哪个平台正规安全,怎么辨别?
VBA驱动SAP GUI实现办公自动化(二):判断元素是否存在
漫画:如何实现大整数相乘?(整合版)
神经网络自我认知模型
mysql5.6解析JSON字符串方式(支持复杂的嵌套格式)
7 pratiques devops pour améliorer la performance des applications
解决“双击pdf文件,弹出”请安装evernote程序
This 17-year-old hacker genius cracked the first generation iPhone!
LeetCode每日一题:合并两个有序数组