Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

CMake基础

介绍

CMake是一个跨平台的自动化构建系统,可以根据不同的平台、不同的编译器,生成相应的构建文件(如Makefile或Visual Studio项目文件等),以便进行编译、链接等操作。
CMake通过CMakeLists.txt 文件来描述项目的构建过程,用户可以在这个文件中定义源代码、库、头文件、编译选项等信息,从而让CMake自动生成相应的构建文件。

安装

win

CMake下载
安装完成后,进入系统环境变量设置对话框,将其安装路径写入环境变量Path中。

linux

linux安装指令

sudo apt install cmake

使用

在文件目录下创建CMakeLists.txt文件(大小写敏感)。
CMakeLists.txt中添加对应的配置。
为了使工程结构清晰,可以单独创建对应的src,inc与build文件夹。
进入build文件夹使用以下指令进行构建。
linux:

1
2
3
4
//使用上级目录下的配置文件生成 Makefile
cmake ..
//执行 make 指令生成可执行文件
make

win:

1
2
3
4
//使用上级目录下的配置文件生成 Makefile
cmake ..
//生成可执行文件
mingw32-make.exe

常用的配置命令

1
2
3
4
5
6
7
8
9
10
11
12
# 最低版本
cmake_minimum_required(VERSION 3.10)
# 项目名称
project(XXX)
# 查找源文件
aux_source_directory(路径 变量)
# 规定.h头文件路径
include_directories(路径)
# 生成可执行文件
add_executable(源文件)
# 添加调试信息
add_definitions("-Wall -g")

除此之外,CMake可以使用变量来存储和传递信息。
可以使用set(变量名 变量值)来设置。
通过${变量名}来使用。
CMake还有一些定义好的变量来方便使用:

1
2
3
4
5
6
# 这两变量指代的内容是一致的,都是工程顶层目录。
CMAKE_SOURCE_DIR / PROJECT_SOURCE_DIR
# 当前处理的 CMakeLists.txt 所在的路径
CMAKE_CURRENT_SOURCE_DIR
# 通过 PROJECT 指令定义的项目名称
PROJECT_NAME

与vscode组合使用

与vscode组合使用需安装对应的语言与CMake扩展。
配置好launch.json与tasks.json文件就可以使用cscode进行运行与调试了。

launch.json文件

win:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe 调试配置文件",
"type": "cppdbg",
"request": "launch",
"program": "可执行文件目录",
"args": [],
"stopAtEntry": false,
"cwd": "源文件夹路径",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "gdb路径",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}

linux:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe 调试配置文件",
"type": "cppdbg",
"request": "launch",
"program": "可执行文件目录",
"args": [],
"stopAtEntry": false,
"cwd": "源文件夹路径",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "Build",
"miDebuggerPath": "gdb路径",
}
]
}

tasks.json文件

win:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "mingw32-make.exe",
"args": []
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
]
}
]
}

linux:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
{
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
".."
]
},
{
"label": "make",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": []
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"cmake",
"make"
]
}
]
}

参考资料

cmake 常用变量
CMake 菜鸟教程
个人博客

评论