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.
然而网上并没有好的解决方法。经过一番折腾,包括查阅资料,问人等,终于搞定了这个问题。现将解决方法记录下,如果有网友需要,可以参考我的做法。
二、解决方案
- 本人使用的是最新版本的VS code
- 下载最新版本的mingw64。下载链接为:mingw64。注意一定要下载x86_64-posix-seh这个版本!!!将下载好的文件,解压到想要的位置,并且配置好环境变量。怎么配置mingw,可以自行查询搜索引擎。
- 修改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 加断点,测试是否可以捕获断点

正常捕获断点,可以正常调试。
大功告成!!!
&spm=1001.2101.3001.5002&articleId=123975430&d=1&t=3&u=526f7cadf4474ab698b5a046c1275a28)
2800

被折叠的 条评论
为什么被折叠?



