VS code 配置std::thread调试环境(windows 系统)

一、问题背景

本人已经按照网上的教程配置好一般情况下的VS code的C++开发环境(大家可以自行百度,谷歌等,这个不是本文重点)。在没有使用std::thread之前,写写基本的代码,并且用VS code的F5调试,一切都正常。最近想写点多线程std::thread的代码,然后用VS code调试,出现以下错误:

ERROR: Unable to start debugging. Unexpected GDB output from command "-exec-run". During startup program exited with code 0xc0000139.

然而网上并没有好的解决方法。经过一番折腾,包括查阅资料,问人等,终于搞定了这个问题。现将解决方法记录下,如果有网友需要,可以参考我的做法。

二、解决方案

  1. 本人使用的是最新版本的VS code
  2. 下载最新版本的mingw64。下载链接为:mingw64。注意一定要下载x86_64-posix-seh这个版本!!!将下载好的文件,解压到想要的位置,并且配置好环境变量。怎么配置mingw,可以自行查询搜索引擎。
  3. 修改VS code的.vscode文件夹下的各个配置文件。
    c_cpp_properties.json文件的配置如下:
{
    "configurations": [
        {
            "name": "Win64-MinGW-w64",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "D:\\mingw64\\bin\\g++.exe", //这里改为你自己的mingw路径
            "cStandard": "c17",
            "cppStandard": "c++20",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

launch.json文件配置如下:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            // "cwd": "${fileDirname}", 这个地方比较关键,换成下面的一句话就行,注意改为自己的mingw路径
            "cwd": "D:\\mingw64\\bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe", //换路径
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe 生成活动文件"
        }
    ]
}

setting.json配置如下:这个不用变,用默认的就好。

{
    "files.associations": {
        "ostream": "cpp",
        "memory": "cpp",
        "vector": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "chrono": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "ratio": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "typeinfo": "cpp"
    }
}

task.json配置如下:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe 生成活动文件",
            "command": "D:\\mingw64\\bin\\g++.exe", //改路径
            "args": [
                "-Wall",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                // "cwd": "${fileDirname}" 
                "cwd": "D:\\mingw64\\bin" //改路径
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

三、示例代码与运行结果

3.1 示例代码

#include <thread>
#include <iostream>

void my_print()
{
    std::cout << "my thread start " << std::endl;
}

int main()
{
    std::thread my_thread(my_print);
    my_thread.join();
    std::cout << "main 函数执行结束" << std::endl;
    return 0;
}

3.2 不加断点,直接F5运行

运行结果

3.2 加断点,测试是否可以捕获断点

捕获断点
正常捕获断点,可以正常调试。

大功告成!!!

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值