当前位置:网站首页>Cmake tutorial series -04- compiling related functions
Cmake tutorial series -04- compiling related functions
2022-06-30 02:37:00 【Ordinary people who like playing basketball】
List of articles
1. introduce
The compilation process and the linking process need to Compiler and linker Pass in different parameters , These parameters fall into the following categories :
- Compilation options ( Including macro definitions )
- Header file path
- Link library file name
- Link library lookup path
2.target_compile_options
This function adds compilation options to the target ,
- Its prototype is :
target_compile_options(<target> [BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_compile_options(sample PUBLIC /arch=avx2 /Wall)
It should be noted that , If you want to set c / c++ standard , Not only can you use this function to add -std=STANDARD, It is also possible and more recommended to set the following two cmake Preset macro values :
- Just fill in the number of the standard number ,cmake Standard related options will be automatically set .
CMAKE_C_STANDARD
CMAKE_CXX_STANDARD
2.target_compile_definitions
This function adds a preset macro declaration and definition to the target .
- Of course , You can also use the above function to do this , After all, the parameters in the command line are not ?
- But professional people do professional things , It is recommended to use this function to add macro definitions . Its prototype is :
target_compile_definitions(<target>
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_compile_definitions(sample PRIVATE BUILD_DLL PUBLIC "-DPI=3.14159")
3.target_compile_features
This function sets... Specifically to the target c / c++ edition . Of course , You can still choose between the two approaches in the first function .
- Its prototype is :
target_compile_features(<target> <PRIVATE|PUBLIC|INTERFACE> <feature> [...])
- for example :
target_compile_features(sample PUBLIC cxx_std_17)
4.target_precompile_headers
This function adds a precompiled header file to the target .
- Is the use of Visual Studio Automatically created after creating a new project pch.h This kind of document .
- Its prototype is :
target_precompile_headers(<target>
<INTERFACE|PUBLIC|PRIVATE> [header1...]
[<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
- eg:
target_precompile_headers(sample PRIVATE pch.hpp)
5.target_include_directories
This function declares the path to use the header file when compiling the target .
- Its prototype is :
target_include_directories(<target> [SYSTEM] [AFTER|BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_include_directories(sample PUBLIC public/include/sample PRIVATE sample)
6.target_link_libraries
This function declares the name of the dependent library or target. Its name can contain the full path .
- Its prototype is :
target_link_libraries(<target>
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_link_libraries(sample PUBLIC CURL::curl glib m)
Be careful : Due to a target Contains multiple attributes , Generally, the header file path is included .
- So use target When this function is passed as an argument , Don't need to call target_include_directories Once again, add the header file path .
7.target_link_directories
This function declares When linking , Find the path to the dependent library .
- Its prototype is :
target_link_directories(<target> [BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_link_directories(sample PUBLIC third_party/libs/x86/rel)
8.target_link_options
This function declares the option to add additional links .
- Its prototype is :
target_link_options(<target> [BEFORE]
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_link_options(sample PUBLIC /shared)
9.target_sources
This function declares to target Add source file .
- Its prototype is :
target_sources(<target>
<INTERFACE|PUBLIC|PRIVATE> [items1...]
[<INTERFACE|PUBLIC|PRIVATE> [items2...] ...])
- eg:
target_sources(sample PRIVATE decode.cxx)
notes : Don't think this function is completely useless , Can be add_library / add_executable replace . In some cases , This function is very easy to use .
- eg:
I need to provide a library containing only source code for downstream use , Instead of compiling the binary and providing it to the downstream . here , We need to use add_library(sample INTERFACE) And use this function to add a source file to link to sample , And pass cmake Export this target. In this way , Downstream only need to use target Family functions can use the source files provided by me without any additional code .
10.target Family function considerations
When needed cmake export Keyword export declared target With the PUBLIC Attribute , We have to take PUBLIC / PRIVATE / INTERFACE Keyword to this kind of target Family function complement . And if one target Family functions declare one of these three keywords , The target Other target All family functions should declare keywords . And for values that contain paths , We need to declare the scope of this value :
- compile / Use when linking . Absolute path must be used .
- Export to provide... Downstream . Relative paths must be used .
therefore , We usually use the following methods :
target_include_directories(sample PRIVATE $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/header/include> PUBLIC $<INSTALL_INTERFACE:include>)
11. Complete example
cmake_minimum_required(VERSION 3.0)
project(sample CXX)
set(CMAKE_CXX_STANDARD 11)
add_library(sample sample.cpp)
target_include_directories(sample PRIVATE $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/header/include> PUBLIC $<INSTALL_INTERFACE:include>)
target_compile_options(sample PUBLIC /Wall)
target_compile_definitions(sample PRIVATE BUILD_DLL INTERFACE USE_DLL)
target_link_libraries(sample PRIVATE thread m)
- Reference resources :CMake Tutorial series -04- Compile related functions
边栏推荐
- [论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
- Raii memory management
- 什么是X.509证书?X.509证书工作原理及应用?
- Template parameter package and function parameter package
- True love forever valentine's Day gifts
- 2.< tag-动态规划和0-1背包问题>lt.416. 分割等和子集 + lt.1049. 最后一块石头的重量 II
- 2022护网行动在即,关于护网的那些事儿
- Pytorch学习(二)
- 并发请求下如何防重复提交
- 走进江苏作家诗人胭脂茉莉|世界读书日
猜你喜欢

冒泡排序

Unity3D UGUI强制刷新Layout(布局)组件

AutoJS代码能加密吗?YES,AutoJS加密技巧展示

Wechat applet page Jump and parameter transfer

最小栈详解

FDA mail security solution

【postgres】postgres 数据库迁移

Linear algebra Chapter 3 summary of vector and vector space knowledge points (Jeff's self perception)

Merge sort

What are the requirements for NPDP product manager international certification examination?
随机推荐
【干货分享】最新WHQL徽标认证申请流程
如何使用SMS向客户传递服务信息?指南在这里!
RAII内存管理
打造创客教育中精湛技艺
【npm】解决使用npm安装TypeORM的报错问题
Quick sort
How long is the general term of the bank's financial products?
IBM websphere通道联通搭建和测试
Global and Chinese market of subscription revenue management software 2022-2028: Research Report on technology, participants, trends, market size and share
002 color classification
Alphassl digital certificate
什么是证书透明度CT?如何查询CT logs证书日志?
FAQs for code signature and driver signature
三层交换机和二层交换机区别是什么
【postgres】postgres 数据库迁移
Entering Jiangsu writers and poets carmine Jasmine World Book Day
选择排序
LeetCode 3. 无重复字符的最长子串
Creating exquisite skills in maker Education
SSL证书格式转化的两种方法