vscode配置C/C++教程(含常见问题)
零基础配置 VS Code 的 C/C++ 开发环境(Windows & Linux,G++ + GDB)
适用对象:第一次在 VS Code 编写 C/C++,希望一条龙完成安装 → 编译 → 调试的同学。
覆盖范围:
- 仅使用 G++/GCC + GDB(Windows 上通过 MinGW‑w64,Linux 用系统 GCC)
- 不含 Clang、MSVC、macOS
- 关键
.vscode/*.json
文件内已加 🔖 注解,指出需要你手动修改的地方。
文章目录
- 零基础配置 VS Code 的 C/C++ 开发环境(Windows & Linux,G++ + GDB)
- 1 · 背景与目标
- 2 · 环境准备
- 2.1 安装 VS Code
- 2.2 安装 G++ 编译器
- 2.3 (Windows) 将 G++ 加入 PATH(为 vsocde 识别 g++ 命令)
- 3 · 创建示例工程
- 4 · .vscode 配置文件
- 4.1 `c_cpp_properties.json` —— IntelliSense & 头文件
- 4.2 `tasks.json` —— 一键编译
- 4.3 `launch.json` —— GDB 调试
- 5 · 多文件项目 & GDB 实战
- 6 · 常见问题
- 7 · 进阶
- 8 · 结束语
1 · 背景与目标
- 完成「编译 → 运行 → 调试」闭环
- 掌握多文件项目与 GDB 单步调试
2 · 环境准备
2.1 安装 VS Code
- 访问 https://code.visualstudio.com/ 下载并安装。
- 打开 Extensions 侧边栏,安装:
- C/C++ (Microsoft) — IntelliSense & 调试
- Code Runner(可选)— 单文件快速运行
2.2 安装 G++ 编译器
操作系统 | 安装方式 |
---|---|
Windows | 下载 MinGW‑w64 (https://sourceforge.net/projects/mingw-w64/files/Toolchains targetting Win64/Personal Builds/mingw-builds/8.1.0/threads-win32/seh/),解压到 C:\Program Files (x86) (根据自己情况选择) |
Linux (Ubuntu) | sudo apt update && sudo apt install build-essential |
执行 g++ --version
验证安装。
2.3 (Windows) 将 G++ 加入 PATH(为 vsocde 识别 g++ 命令)
-
资源管理器定位
C:\Program Files (x86)\mingw64\bin
-
右键「此电脑」→ 属性 → 高级系统设置 → 环境变量(或开始菜单搜env)
-
编辑 系统变量 里的
Path
→ 新建C:\mingw64\bin
→ 确定 -
重开 PowerShell/CMD 再运行
g++ --version
确认生效。
3 · 创建示例工程
1. 新建项目文件夹:
2. 打开vscode:
3. 打开项目文件夹:
4. 新建源文件:
在 main.cpp
写入:
#include <iostream>
int main() {std::cout << "Hello, GCC & VS Code!" << std::endl;return 0;
}
4 · .vscode 配置文件
在工作区根目录新建
.vscode/
文件夹,放置以下 3 个 JSON 文件。
4.1 c_cpp_properties.json
—— IntelliSense & 头文件
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.22621.0","compilerPath": "C:/Program Files (x86)/mingw64/bin/g++.exe","cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "gcc-x64"},{"name": "C++","includePath": ["${workspaceFolder}/**"],"defines": ["_DEBUG","UNICODE","_UNICODE"],"windowsSdkVersion": "10.0.22621.0","compilerPath": "C:/Program Files (x86)/mingw64/bin/g++.exe","cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "gcc-x64"}],"version": 4
}
4.2 tasks.json
—— 一键编译
{"version": "2.0.0","tasks": [{"type": "cppbuild","label": "C/C++: g++.exe 生成活动文件","command": "C:/Program Files (x86)/mingw64/bin/g++.exe","args": ["-fdiagnostics-color=always","-g","${workspaceFolder}\\*.cpp","-o","${workspaceFolder}\\main.exe"],"options": {"cwd": "${workspaceFolder}"},"problemMatcher": ["$gcc"],"group": "build","detail": "编译器: C:/Program Files (x86)/mingw64/bin/g++.exe"}]
}
运行 Ctrl+Shift+B
进行编译,出现main.exe说明被成功编译。
此时按下 Ctrl+` ,输入./main.exe可执行
4.3 launch.json
—— GDB 调试
{"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}\\${workspaceRootFolderName}.exe","args": [],"stopAtEntry": false,"cwd": "${fileDirname}","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "C:\\Program Files (x86)\\mingw64\\bin\\gdb.exe","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true},{"description": "将反汇编风格设置为 Intel","text": "-gdb-set disassembly-flavor intel","ignoreFailures": true}]},{"name": "C/C++: g++.exe 生成和调试活动文件","type": "cppdbg","request": "launch","program": "${workspaceFolder}\\main.exe","args": [],"stopAtEntry": false,"cwd": "C:/Program Files (x86)/mingw64/bin","environment": [],"externalConsole": true,"MIMode": "gdb","miDebuggerPath": "C:\\Program Files (x86)\\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 生成活动文件"}]
}
5 · 多文件项目 & GDB 实战
├── main.cpp
├── add.cpp
└── add.h
add.h
#pragma once
int add(int a, int b);
add.cpp
#include "add.h"
int add(int a, int b) { return a + b; }
main.cpp
#include <iostream>
#include "add.h"
int main() {std::cout << "2 + 3 = " << add(2, 3) << std::endl;return 0;
}
- 在
tasks.json
中使用 build all 任务一次性编译。 F5
启动 GDB ,常用命令:n
、s
、print var
、bt
、continue
。
6 · 常见问题
症状 | 原因 | 解决 |
---|---|---|
g++ 不是内部或外部命令 | PATH 未配置 | 按 2.3 步骤添加后重启终端 |
iostream: No such file | includePath 不全 | 检查 compilerPath 与头文件路径 |
断点无法命中 | 未加 -g 或程序路径不一致 | 确保编译参数含 -g ,launch.json 指向正确 |
中文输出乱码 (Win) | 终端编码 | chcp 65001 或改用 Windows Terminal |
7 · 进阶
- clang-format +
editor.formatOnSave
统一代码风格 - Ccache 加速重编译 (
apt install ccache
) - CMake Tools 管理大型项目
8 · 结束语
恭喜,你已在 VS Code 完成 G++/GDB 全链路配置并成功运行:
如有疑问,欢迎留言讨论,觉得有用请点赞收藏!