1、msys2下载编译器和cmake工具
pacman -S mingw-w64-x86_64-toolchain
pacman -S mingw-w64-x86_64-cmake
toolchain包中包含很多不必要的工具包,应该可以指定具体的工具g++,gcc,mingw32-make的下载,详细命令请自行搜索。
2、将 msys2路径添加到环境变量中
msys2项目中会有几种不同的编译工具clang,mingw,ucrt这里只需要将你想用的工具集的bin目录添加即可
3、验证是否添加成功
cmake --version
g++ -v
gcc -v
mingw32-make -v
4、配置vscode(无需插件版)
4.1 了解cmake编译代码生成可执行文件的核心命令
总的来说就只有两条核心命令
cmake -S . -B build -G MinGW Makefiles
-S 指定CMakeLists.txt与执行命令目录的相对路径
-B 中间文件的生成目录
-G 指定生成器,常用的有MinGW Makefiles, Ninja,Visual Studio 16 2019等
cmake --build build
根据生成的中间文件构建可执行文件
4.2 根据命令配置开发环境
对于vscode不下载插件且能很方便的使用cmake功能主要是依靠tasks.json与lanuch.json文件。编写tasks.json文件后,点击终端->运行任务->选择想要执行的任务。
- tasks.json
{"version": "2.0.0","options": {"cwd": "${workspaceFolder}/build/"// "cwd": "${workspaceFolder}/"},"tasks": [{"label": "cmake","type": "shell","command": "cmake","args": ["..","-G","MinGW Makefiles"]},{"label": "make","group":{"kind":"build","isDefault":true},// "command":"mingw32-make.exe",// "command":"ninja","command":"cmake","args":["--build" ,"." ]},{"label":"Build my project","dependsOn":["cmake","make" ]}]
}
- lanuch.json
{// 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}\\build\\testfile.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": true,"MIMode": "gdb",// "miDebuggerPath": "D:/MinGW64/bin/gdb.exe","miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}],"preLaunchTask": "Build my project"}]
}