CMake快速上手

一:CMake概述

Cmake是跨平台的构建配置工具,并且语法简单,功能强大,IDE支持度高,是C/C++事实上的构建标准。

二:CMake安装

CMake官方源代码下载地址
CMake官方英文参考文档

我的linux系统内核版本:

Linux hcss-ecs-0598 5.4.0-218-generic #238-Ubuntu SMP Mon May 19 10:42:47 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

正常来讲,系统里面因该是自带cmake的,唯一的问题可能是版本有点低

bash 复制代码
zjh@hcss-ecs-0598:~/git_linux_test$ cmake --version
cmake version 3.16.3

如果想升级版本:可以使用如下命令:

bash 复制代码
sudo snap install cmake --classic

执行完之后你就能直接用 /snap/bin/cmake,系统会自动链接到 cmake

三:CMake与makefile对比

项目 CMake Makefile
本质 构建配置生成器(会自动生成 Makefile/VS/Xcode) 构建脚本本身(直接给 make 执行)
抽象层级 高层抽象:描述"我要编译什么" 低层脚本:你得告诉它"怎么编译"
跨平台能力 极强:Linux、Windows、macOS 全通吃 基本只适合 Unix/Linux
维护成本 大项目成本低(自动管理依赖、库、路径) 大项目成本爆炸(规则太多)
写法感受 结构化、现代化 古早味强,容易踩坑(缩进、tab、变量习惯不同)
依赖管理 内置 find_package、target_link_libraries,非常方便 手动指定路径,容易出错
适合项目规模 中大型项目、多人合作、跨平台库 小型项目、简单 Demo
生态支持 主流 C++ 库都提供 CMake 示例 越来越少,逐渐退出主流
生成的输出 Makefile / VS 工程 / Ninja / Xcode 只能生成 Make 构建流程
学习成本 稍微有点学习门槛 简单上手但难写好大型项目

四:牛刀小试

1. 创建文件main.cpp

bash 复制代码
#include<iostream>


int main()
{
    std::cout<<"hello,world"<<std::endl;


    return 0;
}

2. 创建文件CMakeLists.txt

注意:CMakeLists.txt这个文件名字是不能够改变的

bash 复制代码
# 1.设置cmake能够运行的最低版本

cmake_minimum_required(VERSION 3.18)

# 2.项目名称

project(HelloWorld)

# 3.添加构建目标

add_executable(main main.cpp)
  • 设置最低运行版本的原因:这样系统可以检测此时的版本,如果低于你所设置的版本就会报错,这样可以防止你写了一个函数,而这个函数的版本是高于目前的系统版本的。

命令行操作:

bash 复制代码
zjh@hcss-ecs-0598:~/git_linux_test/11-30$ ls
CMakeLists.txt  main.cpp


zjh@hcss-ecs-0598:~/git_linux_test/11-30$ cmake . //在当前文件夹运行cmake


-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/zjh/git_linux_test/11-30
zjh@hcss-ecs-0598:~/git_linux_test/11-30$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  main.cpp  Makefile

zjh@hcss-ecs-0598:~/git_linux_test/11-30$ make

[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
[100%] Built target main
zjh@hcss-ecs-0598:~/git_linux_test/11-30$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  main  main.cpp  Makefile

zjh@hcss-ecs-0598:~/git_linux_test/11-30$ ./main 

hello,world
相关推荐
zzzsde1 小时前
【C++】异常:概念及使用
开发语言·c++·算法
ttod_qzstudio1 小时前
将 AntV X6 图编辑器转化为表达式引擎:Vue3 技术方案深度解析之Kimi
编辑器·antvx6
ttod_qzstudio1 小时前
基于 AntV X6 的图形化数值计算表达式编辑器设计与实现之DeepSeek
编辑器·antvx6
量子炒饭大师1 小时前
【一天一个计算机知识】—— 【编程百度】悬空指针
c语言·数据结构·c++·git·安全·github·dubbo
my烂笔头1 小时前
vscode\cursor集成plantuml方法
ide·vscode·编辑器·uml
矜辰所致1 小时前
C 语言 —— 函数指针
c语言·开发语言·指针·typedef·函数指针
zore_c1 小时前
【C语言】struct结构体内存对齐和位段(超详解)
c语言·开发语言·经验分享·笔记
Chrikk1 小时前
【下篇】C++20 约束、NCCL 通信与并发模型
c++·c++20·c++40周年
MC皮蛋侠客1 小时前
C++17多线程编程全面指南
开发语言·c++