目录
一、前言
1.1 场景:我们要编译我们的⼯程
1.2 ⽩话CMake-真实世界⾥的例⼦
二、CMake 快速开始
2.1 CMake 安装
2.2 VS Code CMake 插件安装
2.3 快速样例 - hello world ⼯程
三、CMake 命令⾏⼯具介绍
3.1 CMake ⼯程构建流程图
3.2 ⽣成构建系统
3.3 编译链接
3.4 测试
3.5 安装
2.6 打包
3.7 脚本模式
2.8 调用外部命令
2.9 查看帮助
一、前言
1.1场景:我们要编译我们的⼯程
1.2⽩话CMake-真实世界⾥的例⼦
优势 | 传统⽅式 | CMake⽅式 | 改进效果 |
解决跨平台构建难 题 | ⼈⼯编辑Makefile等配置 ⽂件 | CMake⾃动帮我们⽣成构建配 置⽂件 | ⼀处配置,到处构建 |
语法简单易上⼿ | Makefile 等语法复杂 | 语法简单,表达能⼒强⼤ | ⼤幅减少学习成本,提升 研发效率 |
解决包管理难题 | ⼿动查找包 | ⾃动查找包 | 包管理规规范化 |
IDE对CMake⽀持 度⾼ | 每个IDE都有⾃⼰的构建 ⽅式 | 各个IDE都⽀持使⽤cmake来构 建程序 | ⼀处配置,多IDE⽀持。 |
结论:CMake 语法简单易上⼿,功能强⼤,使⽤⼴泛,IDE⽀持度⾼,已经是C/C++事实上的构建标准,也是⼀个⼗分重要的C/C++⼯程管理⼯具。学好CMake,不但使你的C/C++⼯程优雅⾼效,也能提升你的个⼈竞争⼒。
二、CMake 快速开始
2.1CMake 安装
所有cmake命令执⾏环境为:编辑环境:VS Code编译环境:VS Code Remote SSH模式 + Ubuntu 24.04
sudo apt install cmake
安装完成后,可通过以下命令验证 CMake 是否安装成功以及查看其版本。cmake --versioncmake version 3.28.3CMake suite maintained and supported by Kitware (kitware.com/cmake).
不同的Linux发行版可能有些不支持。我这里最低版本要求是3.18。
2.2VS Code CMake 插件安装
步骤如下:
2.3快速样例 - hello world ⼯程
tree hello_world├──CMakeLists.txt└──main.cpp
#include<iostream>intmain(){std::cout <<"hello world!"<< std::endl;return0;}
# 1设置能运⾏此⼯程的cmake的最低版本cmake_minimum_required(VERSION 3.18)# 2设置项⽬名称project(helloWorld)# 3添加构建⽬标add_executable(main main.cpp)
#运⾏CMake命令 就在这⼀步 ⽣成Makefilecmake ..-- The C compiler identification is AppleClang 17.0.0.17000013-- The CXX compiler identification is AppleClang 17.0.0.17000013-- Detecting C compiler ABI info-- Detecting C compiler ABI info - done.....-- Build files have been written to:/Users/lixiaoshen/Desktop/CMakeDemo/hello_world/build
都是在终端里面运行。
make[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o[100%] Linking CXX executable main[100%] Built target main
./hellolixiaoshen@lixiaoshens-MacBook-Pro build % ./hellohello world!lixiaoshen@lixiaoshens-MacBook-Pro build %
三、CMake 命令⾏⼯具介绍
3.1CMake ⼯程构建流程图
块# 1. 创建构建⽬录并进⼊ mkdir build && cd build # 2. 配置项⽬ cmake .. # 3. 构建项⽬ make 或者 cmake --build . # 4. 执⾏测试(如果有) make test 或者 ctest . # 5. 安装项⽬ make install 或者 cmake --install . # 6. ⽣成安装包 make package 或者 cpack # 7 解压 ⽣成的tar 包 tar xvf TestCMakeTools-0.1.1-Linux.tar.gz├──build├──CMakeLists.txt├──main.cpp└──test.cpp
#include<iostream>intmain(){std::cout <<"hello world!"<< std::endl;return0;}
#include<iostream>#include<cassert>intmain(){assert(1+2==3);std::cout <<"test passed!"<< std::endl;return0;}
# 1 设置能运⾏此cmake ⼯程的最低cmake版本要求 cmake_minimum_required(VERSION 3.18) # 2 设置项⽬名称 project(helloWorld) # 3 添加构建⽬标 # g++ main.cpp -o main add_executable(main main.cpp) #⽣成测试⼆进制可执⾏程序 add_executable(testAdd test.cpp) # 4 开启测试功能 & 集成测试逻辑 include(CTest) add_test( NAME Case_Add COMMAND testAdd ) # 5 安装⼆进制可执⾏程序到本地 include(GNUInstallDirs) install(TARGETS main) # 6 开启打包功能 & 打包⼆进制可执⾏程序 include(CPack) # cpack 默认收集install 对应的⽬标,然后会把收集到的⽬标 打包在压缩包⾥3.2⽣成构建系统
cmake [options] <path-to-source>cmake [options] <path-to-existing-build>cmake [options] -S <path-to-source> -B <path-to-build>
-S:指定源⽂件根⽬录,必须包含⼀个CMakeLists.txt⽂件
-B: 指定构建⽬录,构建⽣成的中间⽂件和⽬标⽂件的⽣成路径,通常会保存⼀个CMakeCache.txt来存储⼀些变量。
cmake .
cd build && cmake ../隐含./为构建⽬录../为源⽂件⽬录
prefix =/usr/localCMAKE_INSTALL_PREFIX=${prefix}
3.3编译链接
cmake --build <dir> = Build a CMake-generated project binarytree.
cmake --build ./或者make[ 25%] Building CXX object CMakeFiles/main.dir/main.cpp.o[ 50%] Linking CXX executable main[ 50%] Built target main[ 75%] Building CXX object CMakeFiles/testApp.dir/test.cpp.o[100%] Linking CXX executable testApp[100%] Built target testApp
3.4测试
Usagectest [options]
ctest或者maketestTest project /home/bit/workspace/CMakeClass/cmake_tools/buildStart 1: testAdd1/1 Test#1: testAdd .......................... Passed 0.00 sec100% tests passed, 0 tests failed out of 1Total Test time (real) = 0.00 sec
3.5 安装
cmake --install <dir> = Install a CMake-generated project binary
cmake --install .或者make install[100%] Built target mainInstall the project...-- Install configuration:""-- Installing: /usr/local/bin/main
2.6 打包
Usagecpack [options]
cpack 或者 make package CPack: Create package using STGZ CPack: Install projects CPack: - Run preinstall target for: TestCMakeTools CPack: - Install project: TestCMakeTools [] CPack: Create package CPack: - package: /home/bit/workspace/CMakeClass/cmake_tools/build/TestCMakeTools-0.1.1-Linux.sh generated. CPack: Create package using TGZ CPack: Install projects CPack: - Run preinstall target for: TestCMakeTools CPack: - Install project: TestCMakeTools [] CPack: Create package CPack: - package: /home/bit/workspace/CMakeClass/cmake_tools/build/TestCMakeTools-0.1.1- Linux.tar.gz generated. CPack: Create package using TZ CPack: Install projects CPack: - Run preinstall target for: TestCMakeTools CPack: - Install project: TestCMakeTools [] CPack: Create package CPack: - package: /home/bit/workspace/CMakeClass/cmake_tools/build/TestCMakeTools-0.1.1- Linux.tar.Z generated.3.7 脚本模式
cmake -P <file> = Process script mode.
# Special rule for the target installinstall: preinstall@$(CMAKE_COMMAND) -E cmake_echo_color"--switch=$(COLOR)"--cyan"Installthe project..."/usr/local/bin/cmake -P cmake_install.cmake.PHONY : install
2.8 调用外部命令
cmake -E = CMake command mode.
# The command to remove a file.RM = /usr/bin/cmake -Erm-f
2.9 查看帮助
cmake --help Usage cmake [options] <path-to-source> cmake [options] <path-to-existing-build> cmake [options] -S <path-to-source> -B <path-to-build> Specify a source directory to (re-)generate a build system for it in the current working directory. Specify an existing build directory to re-generate its build system. Options -S <path-to-source> = Explicitly specify a source directory. -B <path-to-build> = Explicitly specify a build directory. -C <initial-cache> = Pre-load a script to populate the cache. -D <var>[:<type>]=<value> = Create or update a cmake cache entry. -U <globbing_expr> = Remove matching entries from CMake cache. -G <generator-name> = Specify a build system generator. -T <toolset-name> = Specify toolset name if supported by generator. ............