cmake 02 hello

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

cmake 学习笔记

一个最小化的 cmake 项目

目录结构

F:\2023\code\cmake\hello_cmake>tree /f
卷 dox 的文件夹 PATH 列表
卷序列号为 34D2-6BE8
F:.
│  CMakeLists.txt
│  main.cpp
│
└─.vscode
        launch.json
        tasks.json


F:\2023\code\cmake\hello_cmake>

源码

main.cpp

#include <iostream>

int main()
{
    int a = 1;
    a = 2;
    a = 3;
    a = 4;
    std::cout << "hello world" << std::endl;
    return 0;
}

CMakeLists.txt

# 该项目所需 cmake 的最小版本, 如果 cmake 版本小于设置的版本,  cmake 将停止处理并报错
cmake_minimum_required(VERSION 3.0)

# 设置项目名称和语言
project(hello_cmake CXX)
set(CMAKE_BUILD_TYPE DEBUG)

# 指定 c++ 11
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

# 使用指定的源文件向项目添加可执行文件
add_executable(${PROJECT_NAME} main.cpp)

如何运行

通常来说, 一个 cmake 的 hello world 的运行步骤如下:

# 创建构建目录
md build
# 进入构建目录
cd build
# 创建 mingw 的 makefile
cmake .. -G "MinGW Makefiles"
# 生成可执行程序
make
# 运行
hello_cmake.exe

但是实际上现在我们可以用一条命令直接搞定

cmake -S . -G "MinGW Makefiles" -B build && cmake --build build

有什么问题

  1. MinGW 其实有问题, 当然也可能是我用的版本不对, 在 vscode 中 debug 的时候看不到变量的值
  2. 如果不能同时在 linux/windows/mac os 下构建, 那么 cmake 的威力砍了一半
  3. 如何与vs code 集成? 我想用 Mingw 怎么办? 我想用 tdm-gcc 怎么办? 我想用 msvc 怎么办?
  4. 如何与vs集成?

windows vscode + tmd-gcc

环境配置

tmd-gcc 的配置参考: https://blog.csdn.net/weixin_48820458/article/details/128069360

vscode 参考:
手把手教会VSCode的C++环境搭建多文件编译Cmakejson调试配置 Windows篇
基于CMake的VSCode下的 C/C++环境搭建_Window篇

建议
vscodecmake 插件和 cmake-tools 插件其实没有什么用, 安装一个CMake Language Support有个语法提示就差不多够了.

操作流程

vscode 配置

首先用 vscode 打开顶层CMakeLists.txt所在目录
然后在 .vscode 中添加如下配置文件
launch.json

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "debug",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/hello_cmake.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\program\\tdmgcc\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "Build My Project"
        }
    ]
}

tasks.json

{
    "version": "2.0.0",
    "options": {
        "cwd": "${workspaceFolder}"
    },
    "tasks": [
        {
            "type": "shell",
            "label": "configuration",
            "command": "cmake",
            "args": [
                "-S",
                ".",
                "-G",
                "Unix Makefiles",
                "-B",
                "build"
            ]
        },
        {
            "type": "shell",
            "label": "build",
            "command": "cmake",
            "args": [
                "--build",
                "build",
                "--config",
                "debug"
            ]
        },
        {
            "label": "Build My Project",
            "dependsOn":[
                "configuration",
                "build"
            ]
        }
    ],
}

关于命令行

以下命令均在 vscode 的终端中运行

其实tasks的作用就相当于调用了如下命令:

cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug

但是在我的电脑上显示是错误的

F:\2023\code\cmake\hello_cmake>cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 8.1.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/program/MinGW/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: F:/2023/code/cmake/hello_cmake/build
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.obj
[100%] Linking CXX executable hello_cmake.exe
[100%] Built target hello_cmake

F:\2023\code\cmake\hello_cmake>

可能是我之前安装过 MinGW , 然后一直读取的旧的 gcc , 但其实我的环境变量已经去掉了MinGW并配置了tdm-gcc,此时我们可以手动设置一个环境变量

F:\2023\code\cmake\hello_cmake>rd /q/s build

F:\2023\code\cmake\hello_cmake>set CXX=D:/program/tdmgcc/bin/g++.exe

F:\2023\code\cmake\hello_cmake>cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 10.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/program/tdmgcc/bin/g++.exe - skipped
-- Detecting CXX compile features       
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: F:/2023/code/cmake/hello_cmake/build
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.obj
[100%] Linking CXX executable hello_cmake.exe
[100%] Built target hello_cmake

F:\2023\code\cmake\hello_cmake>

debug

此时已经配置正确, vscode终端已经构建的前提下, 直接F5
在这里插入图片描述
现在我们已经可以用 vscode + tdm-gcc + cmake 进行 c艹开发了

windows Visual Studio 2019 + cmake

直接 debug 跑起来

main.cppCMakeLists.txt复制到一个新的文件夹, 然后用 vs2019直接打开这个文件夹, 并耐心等待编译完毕, 编译完毕之后大约如下
在这里插入图片描述

启动项选择可执行文件
在这里插入图片描述

直接 F5
在这里插入图片描述

如何构建 Release

点击管理配置
在这里插入图片描述
或者顶层CMakeLists.txt右键
在这里插入图片描述

点击加号
在这里插入图片描述
选择 x64-Release
在这里插入图片描述
CMakeLists.txtset(CMAKE_BUILD_TYPE DEBUG) 注释掉, 否则无法构建 Release 版本

然后选中 x64-Release F5 即可
注意: 此时如果有断点, 其实依然可以 debug

linux

这个是最简单的

ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake$ ll
total 16
drwxrwxr-x 2 ubuntu ubuntu 4096 Jan 21 04:10 ./
drwxrwxr-x 3 ubuntu ubuntu 4096 Jan 21 04:02 ../
-rw-rw-r-- 1 ubuntu ubuntu  429 Jan 21 04:02 CMakeLists.txt
-rw-rw-r-- 1 ubuntu ubuntu  154 Jan 21 04:09 main.cpp
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake$ cmake -S . -B build && cmake --build build
-- The CXX compiler identification is GNU 11.3.0
-- 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
-- Generating done
-- Build files have been written to: /home/ubuntu/code/cmake/hello_cmake/build
[ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[100%] Linking CXX executable hello_cmake
[100%] Built target hello_cmake
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake$ ./build/hello_cmake
hello world
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake$

上述操作有什么问题

  1. 单文件不具备什么实际意义, 还不如vscode自带的方便
  2. 命令行需要敲的命令有点多了, 我们需要一个执行cmake或者生成cmake然后执行cmake的脚本, 此脚本有两种选择, 一种是windows平台写个bat脚本, linux写个shell脚本, 另一种是统一用python. 简单起见, 本文使用 pyton 脚本
  3. 要通过传参的方式控制debug或release, 而不是修改脚本
  4. 要有单元测试
  5. 构建前后有些必要的操作, 这个也是通过 python 脚本来处理

下一篇:
cmake 03 一个可用的 cmake 工程应当是什么样的

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6