[自学笔记] Windows + VSCode + MinGW-w64 + CMake调试

1. 安装VSCode

官网下载VSCode(https://code.visualstudio.com/), 并按需安装C/C++、CMake、CMakeTool、Code Runner等插件。

2. 下载MinGW-w64

下载MinGW-w64运行库(https://winlibs.com/),下载推荐的最新UCRT运行库压缩包。
解压压缩包到本地,并且在环境变量PATH中添加bin路径(此处不赘述)。

3. 安装CMake

官网下载并安装CMake(https://cmake.org/download/),安装时记得加入环境变量。

4. VSCode调试工程配置

VSCode中需要配置tasks.json和launch.json,然后就可以运行了。参考配置如下:

tasks.json

{
    "version": "2.0.0",
    "options": {"cwd": "${workspaceFolder}/build"},
    "tasks": [
        {
            "type": "shell",
            "label": "cmake",
            "command": "cmake",
            "args": [
                "-G",
                "\"MinGW Makefiles\"",
                ".."
            ],
        },
        {
            "label": "make",
            "command": "mingw32-make.exe",
        },
        {
            "label": "build",
            "dependsOrder": "sequence",
            "dependsOn": [
                "cmake",
                "make",
            ]
        },
    ]
}

launch.json

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

注意:需要在CMakeLists.txt中设置为调试模式,调试模式打断点才起效。

set(CMAKE_BUILD_TYPE "Debug")

你可能感兴趣的:([自学笔记] Windows + VSCode + MinGW-w64 + CMake调试)