当前位置:网站首页>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 .
边栏推荐
- 华为云云原生容器综合竞争力,中国第一!
- 如何保存训练好的神经网络模型(pytorch版本)
- Alpha conversion from gamma space to linner space under URP (II) -- multi alpha map superposition
- 漫画:有趣的【海盗】问题
- 数据访问 - EntityFramework集成
- Please tell me why some tables can find data by writing SQL, but they can't be found in the data map, and the table structure can't be found
- About JSON parsing function JSON in MySQL_ EXTRACT
- Tita 绩效宝:如何为年中考核做准备?
- LeetCode每日一题:合并两个有序数组
- Cloud security daily 220705: the red hat PHP interpreter has found a vulnerability of executing arbitrary code, which needs to be upgraded as soon as possible
猜你喜欢

企业数字化发展中的六个安全陋习,每一个都很危险!

leetcode每日一题:字符串中的第一个唯一字符

求解为啥all(())是True, 而any(())是FALSE?

C # mixed graphics and text, written to the database in binary mode

33: Chapter 3: develop pass service: 16: use redis to cache user information; (to reduce the pressure on the database)

Knowledge points of MySQL (7)

哈趣K1和哈趣H1哪个性价比更高?谁更值得入手?

Beijing internal promotion | the machine learning group of Microsoft Research Asia recruits full-time researchers in nlp/ speech synthesis and other directions

ICML 2022 | meta proposes a robust multi-objective Bayesian optimization method to effectively deal with input noise

Short the command line via jar manifest or via a classpath file and rerun
随机推荐
7. Scala class
十个顶级自动化和编排工具
Knowledge points of MySQL (6)
Learn about MySQL transaction isolation level
漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
How MySQL uses JSON_ Extract() takes JSON value
一文了解Go语言中的函数与方法的用法
Kafaka技术第一课
Why is February 28 in the Gregorian calendar
Cartoon: looking for the k-th element of an unordered array (Revised)
MySQL之知识点(七)
Machine learning 01: Introduction
Cartoon: a bloody case caused by a math problem
The comprehensive competitiveness of Huawei cloud native containers ranks first in China!
Database design in multi tenant mode
Thesis reading_ Chinese NLP_ LTP
Cartoon: how to multiply large integers? (integrated version)
Example tutorial of SQL deduplication
tkinter窗口预加载
忽米沄析:工业互联网标识解析与企业信息系统的融合应用