本文主要是介绍C++多线程开发环境配置方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks....
下载安装 MinGW-w64
https://winlibs.com/

必须选择带 “posix” 的版本,这是多线程支持的关键!
解压到 C:\mingw64(路径不要有空格和中文)
配置环境变量
右键"此电脑" → 属性 → 高级系统设置 → 环境变量
在 系统变量 中找到 Path → 编辑 → 新建 → 添加:
C:\mingw64\bin
验证安装
按 Win+R,输入 cmd 回车,执行:
g++ --version
下载安装VS code
安装完成后安装 VS Code 扩展:
打开 VS Code,点击左侧扩展图标(四方块),搜索并安装:
C/C++ - Microsoft(必装)
C/C++ Extension Pack - Microsoft(推荐)
安装后 重启 VS Code。
创建测试项目
在桌面新建文件夹 cpp-thread-test
用 VS Code 打开该文件夹(文件 → 打开文件夹)
新建文件 main.cpp,粘贴以下代码:
#include <IOStream> #include <thread> #include <vector> #include <mutex> std::mutex mtx; void print_thread_id(int id) { std::lock_guard<std::mutex> lock(mtx); std::cout << "Thread " << id << " is running\n"; } int main() { std::vector<std::thread> threads; for (int i = 0; i < 5; ++i) { threads.emplace_back(print_thread_id, i); } for (auto& th php: threads) { th.join(); } std::cout << "All threads completed!" << std::endl; system("pause"); // 防止闪退 return 0; }
配置编译任务
创建 tasks.json
按 Ctrl+Shift+P,输入 “Tasks: Configure Default Build Task” → 选择 “Create tasks.json file from template” → 选择www.chinasem.cn “Others”。
将生成的文件内容 全部替换 为:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C++ multi-thread",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17",
"-pthread"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "编译带线www.chinasem.cn程支持的 C++ 程序"
}
]
}关键参数说明:
-g:生成调试信息
-std=c++17:使用 C++17 标准
-pthread:启用多线程支持
创建 launch.json
点击左侧运行和调试图标(▶️)→ 点击 “创建 launch.json 文件” → 选择 &lhttp://www.chinasem.cndquo;C++ (GDB/LLDB)” → 选择 “g++.exe - 生成和调试活动文件”。
将生成的文件内容 全部替换 为:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug Multi-thread",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true, // 使用外部控制台防止闪退
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe", // 按实际路径修改
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchtask": "build C++ multi-thread"
}
]
}手工配置编译任务
如果在上一步的配置调试设置
这里没有看到: 选择 “g++.exe - 生成和调试活动文件”。
因为 VS Code 没检测到 g++ 编译器。终极解决方案:手动创建配置
目文件夹里,找到 .vscode 文件夹,删除里面所有文件(launch.json, tasks.json 等)
创建 tasks.json
右键 .vscode 文件夹 → 新建文件 → 命名为 tasks.json → 粘贴:
{
"version": "2.0.0",
"tasks": [
{
"label": "build C++ multi-thread",
"type": "shell",
"command": "g++",
"argpythons": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-std=c++17",
"-pthread"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"]
}
]
}创建 launch.json
右键 .vscode 文件夹 → 新建文件 → 命名为 launch.json → 粘贴:
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Debug Multi-thread",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build C++ multi-thread"
}
]
}新建项目时,直接复制整个 .vscode 文件夹到新项目根目录
到此这篇关于C++多线程开发环境配置方法的文章就介绍到这了,更多相关C++多线程环境内容请搜索China编程(www.chinasem.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程China编程(www.chinasem.cn)!
这篇关于C++多线程开发环境配置方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!