0%

cmake入门过程记录

随着cmake的学习过程持续更新。

下载安装

下载网址:https://cmake.org/download/
.msi提供的是命令行版的cmake,安装时添加到PATH里,然后在cmd里即可使用cmake的功能(例如cmake .等)
.zip下载后提供的是GUI版的cmake

作用简介

通过一个名为CMakeLists.txt的文件(文件中有cmake脚本)起到编译链接的作用,将零散的源文件整合成一个工程。
makefile也可起到这个作用,但cmake更简单一些。
如果使用的VS这种大型IDE的话,IDE会自动帮你完成编译链接,这时就用不到cmake了。

写第一个cmake脚本

https://blog.csdn.net/zhangyiant/article/details/51289404

进阶

中文:https://blog.csdn.net/weixin_39408343/article/details/102951335
官方文档:https://cmake.org/cmake/help/v3.16/guide/tutorial/index.html

遇到的一些bug记录

VERSION not allowed unless CMP0048 is set to NEW

解决方案:
在“project (Tutorial VERSION 1.0)”语句之前添加如下语句:

1
2
3
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)

解决方案原网址:https://github.com/Tencent/rapidjson/issues/1154

一个范例CMakeLists.txt

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
cmake_minimum_required (VERSION 2.6)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#防止“project (Tutorial VERSION 1.0)”中的“VERSION 1.0”产生CMP0048相关bug
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
endif (POLICY CMP0048)


project (Tutorial VERSION 1.0)

configure_file(TutorialConfig.h.in TutorialConfig.h)

#使得主程序在include文件时也进入这个文件夹(即工程所在文件夹)搜索,从而将TutorialConfig.h添加入工程
include_directories("${PROJECT_BINARY_DIR}")

#如下两行为TutorialConfig.h.in的内容,在CMakeLists里不起作用
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@



add_executable (Tutorial tutorial.cpp)