当前位置:网站首页>一文读懂CMake
一文读懂CMake
2022-07-27 22:00:00 【CAir2】
这篇文章是站在VS角度描述CMake语法的。具体语法对应关系,参照下表。
CMake函数对应VS相关设置
| VS相关设置 | CMake函数 |
|---|---|
| 指定包含头文件目录 | target_include_directories |
| 指定链接库目录 | target_link_directories |
| 指定链接库 | target_link_libraries |
| 指定C++标准 | set(CMAKE_CXX_STANDARD 11) |
| 指定编译器版本 | cmake -G “Visual Studio 12 2013” |
| 指定平台集 | cmake -G “Visual Studio 12 2013 Win64” |
| 指定编译类型 | set(CMAKE_BUILD_TYPE “Debug”)等价于g++ -g |
| 指定预编译宏 | add_definitions(-Dxxx -Dyyy ) 必须-D开头 |
| 指定解决方案名称 | project(xxxx) |
CMake常见内置变量
| CMake内置变量 | 说明(路径都是全路径) |
|---|---|
| PROJECT_NAME | CMakeList.txt里设置的project_name |
| PROJECT_SOURCE_DIR | 根CMakeLists.txt所在的路径 |
| PROJECT_BINARY_DIR | 工程的构建目录(build目录) |
| CMAKE_CURRENT_SOURCE_DIR | 当前’CMakeLists.txt’ 所在的路径 |
| CMAKE_CURRENT_BINARY_DIR | 当前正在处理的’构建目录’ |
| CMAKE_CURRENT_LIST_DIR | 当前处理的’cmake文件’所在的目录 |
| CMAKE_CURRENT_LIST_FILE | 当前处理的’CMakeLists.txt或cmake’文件的全路径 |
| CMAKE_CURRENT_LIST_LINE | 当前处理的’CMakeLists.txt或cmake’文件的’行号’ |
| CMAKE_PROJECT_NAME | 整个项目’配置的project_name |
| LIBRARY_OUTPUT_DIR | 库的存放目录(需要用户设置) |
| BINARY_OUTPUT_DIR | 可执行文件的存放目录(需要用户设置) |
CMake Hellow word
project
project/main.cpp
project/CMakeLists.txt
project/build
project/CMakeLists.txt
#设置CMake版本要求
cmake_minimum_required(VERSION 3.10)
#设置工程名称
project(hellow)
#设置C++11标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
#添加可执行文件
add_executable(hellow main.cpp)
执行CMake指令
#当前目录build
#-G指定编译器Vs2013以及平台工具集类型X64
cmake -G "Visual Studio 12 2013 Win64" ../

指定Debug模式(-g)
set(CMAKE_BUILD_TYPE "Debug")
生成mymath库add_library
project/mymath
project/mymath/mymath.h
project/mymath/mymath.cpp
project/mymath/CMakeLists.txt
project/CMakeLists.txt
#增加一个子目录到编译工程中,子目录应该包含CMakeLists.txt
add_subdirectory(mymath)
#增加头文件包含目录
target_include_directories(hellow PUBLIC
"${PROJECT_SOURCE_DIR}/mymath"
)
#链接lib
target_link_libraries(hellow PUBLIC mymath)
project/mymath/CMakeLists.txt
#添加lib默认是静态库,如果指定SHARED则为动态库
add_library(mymath SHARED mymath.cpp)
此时就会生成mymath库了,下面是完整的project/CMakeLists.txt
#设置CMake版本要求
cmake_minimum_required(VERSION 3.10)
#设置工程名称
project(hellow)
#设置C++11标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
#增加一个子目录到编译工程中,子目录应该包含CMakeLists.txt
add_subdirectory(mymath)
#添加可执行文件
add_executable(hellow main.cpp)
#增加头文件包含目录
target_include_directories(hellow PUBLIC
"${PROJECT_SOURCE_DIR}/mymath"
)
#链接lib
target_link_libraries(hellow PUBLIC mymath)

链接第三方库
下面以链接 d://test//test.lib为例,test.lib的头文件在d://test//include//
#附加test.lib
target_link_libraries(hellow PUBLIC mymath test)
#指定lib库位置
target_link_directories(hellow PUBLIC "d:/test")
#指定lib头文件位置
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
)
下面是完整的project/CMakeLists.txt
#设置CMake版本要求
cmake_minimum_required(VERSION 3.10)
#设置工程名称
project(hellow)
#设置C++11标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
#增加一个子目录到编译工程中,子目录应该包含CMakeLists.txt
add_subdirectory(mymath)
#添加可执行文件
add_executable(hellow main.cpp)
#增加头文件包含目录
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
)
#链接lib
target_link_libraries(hellow PUBLIC mymath test)
#指定链接库的目录
target_link_directories(hellow PUBLIC "d:/test")

增加预定义宏add_definitions
例如:必须定义
USE_OPEN_SSL才能开启openssl
#增加预定义宏,必须-D开头
add_definitions(-DUSE_OPEN_SLL)
多文包含件处理
如果目标里面包含多个文件,如果手动指定则会很麻烦。可以通过aux_source_directory扫描并获取当前目录文件。
#增加sub函数
project/sub.cpp
project/sub.h
#扫描当前目录文件,并且保存在变量all_file中
aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}" all_file)
add_executable(hellow ${
all_file})
配置项目版本号和信息
#设置项目名称和版本
project(Tutorial VERSION 1.0)
#配置头文件传递版本号给源代码
#配置宏定义,配置默认值为ON,如果想关闭 cmake -DUSE_MYMATH=OFF
#option 除了ON之外其他的都默认为OFF
option(USE_MYMATH "user mymath.lib" ON)
#传递字符串给配置文件
set(var_Key "hellow word")
#传递INT给配置文件
set(INT_Key 1)
#输入文件 输出文件
configure_file(hellow_ver.h.in hellow_ver.h)
#由于配置好的文件hellow_ver.h将被写入二进制目录
#所以我们必须将该目录添加到 include 文件的搜索路径中
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
"${PROJECT_BINARY_DIR}"
)
hellow_ver.h.in
//配置的项目版本号
#define hellow_VERSION_MAJOR @[email protected]
#define hellow_VERSION_MINOR @[email protected]
//宏定义
#cmakedefine USE_MYMATH
//注意:${}之间的名称要与cmakedefine后的变量名一样
//变量名称和cmakeLists.txt设置的变量名称相同
#cmakedefine var_Key "@[email protected]”
#cmakedefine INT_Key @[email protected]
运行之后生成的hellow_ver.h
如果想取消USE_MYMATH 可以使用 -DUSE_MYMATH=OFF
cmake -G "Visual Studio 12 2013 Win64" ../ -DUSE_MYMATH=OFF
生成动态库or静态库
#确定mymath.lib是否使用静态库
option(MYMATH_STATIC "user mymath static lib" ON)
if(MYMATH_STATIC)
add_library(mymath STATIC mymath.cpp)
else()
add_library(mymath SHARED mymath.cpp)
endif()
本文参考:CMake 教程,至于CMake的安装和测试将在下一章讲解。
函数简单说明
对于CMake,可能存在两种函数:xxxxx和target_xxxx。下面以
link_libraries和target_link_libraries来做讲解。link_libraries:针对全局的,即:CMake的所有模块都会添加该库。target_link_libraries:针对指定目标的,只有指定目标包含该库。
例如CMake包含连个目标A和B,此时如果通过link_libraries(ws2_32 test)包含ws2_32.lib和test.lib那么A和B都将包含这两个库。但是如果通过target_link_libraries(A PUBLIC ws2_32 test)那么此时仅仅目标A包含这两个库。
边栏推荐
猜你喜欢

The server is poisoned - the dish is the original sin

几行代码轻松实现对于PaddleOCR的实时推理,快来get!

Matlab | those matlab tips you have to know (I)

Fastjson历史漏洞复现

Harmonyos 3 pure mode can limit the access to personal data of risk applications detected in Huawei's application market

智能便利店带你解锁未来科技购物体验

Implement Gobang game with C language

二舅治好我的精神内耗,也让我火出了B站

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
随机推荐
What a beautiful rainbow
Application scenario Display of metauniverse
Is there a general formula for tens of millions of players? B station is underestimated as a hot money opportunity!
Jin's thinking
英特尔携手汉朔、微软,释放“AI + 零售”大招!
The latest notice of the Chinese Academy of Sciences: abandon the impact factor! The journal zoning table will be published for the "Journal surpassing index"
Introduction to thesis writing | how to write an academic research paper
Remote monitoring of pump station
Smart convenience store takes you to unlock the future technology shopping experience
永州二恶英实验室建设细节查看
startUMl
y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
智能便利店带你解锁未来科技购物体验
[C language] string reverse order (recursive implementation)
MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use
《数字经济 科技向善》大咖对谈干货来啦
The server is poisoned - the dish is the original sin
Understand the parental delegation model
҈直҈播҈预҈告҈ |҈ 炎热盛夏,与Nono一起跨越高温“烤”验吧!
阿里二面:为什么要分库分表?