Windows 上使用 VS Code + MSVC 开发 C++ 全流程(含调试)
一、前提:你要安装的工具
安装时请勾选以下内容:
- MSVC C++ 编译器(v14.x)
- Windows 10 SDK(或 11)
- C++ 生成工具(默认就行)
二、VS Code 安装扩展
打开 VS Code → 进入扩展市场,安装:
扩展名称 | 作用 |
---|
C/C++ by Microsoft | 提供 IntelliSense、错误提示、调试支持 |
三、项目目录结构建议
your_project/
├── hello.cpp
├── build_and_run.bat
├── output/
├── build/
└── .vscode/
├── tasks.json
├── launch.json
├── settings.json
└── c_cpp_properties.json
四、核心文件内容
1. build_and_run.bat
@echo off
setlocal enabledelayedexpansion
call "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
set "filename=%~n1"
if not exist build mkdir build
if not exist output mkdir output
if exist build\%filename%.obj del /q build\%filename%.obj
if exist output\%filename%.exe del /q output\%filename%.exe
cl.exe /EHsc /Zi %1 /Fo:build\ /Fe:output\%filename%.exe /link /DEBUG
if exist output\%filename%.exe (
echo -------------------------------
echo Running output\%filename%.exe:
echo -------------------------------
output\%filename%.exe
) else (
echo ❌ Build failed: .exe not created
)
endlocal
2. .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build and Run C++ via .bat",
"type": "shell",
"command": "${workspaceFolder}\\build_and_run.bat",
"args": ["${file}"],
"options": {
"cwd": "${fileDirname}",
"shell": {
"executable": "cmd.exe",
"args": ["/d", "/c"]
}
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": []
}
]
}
3. .vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug EXE in output/",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/output/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"console": "integratedTerminal"
}
]
}
4. .vscode/c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.44.35207/include",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/ucrt",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared",
"C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/um"
],
"defines": [],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "cl.exe",
"intelliSenseMode": "windows-msvc-x64",
"cppStandard": "c++17"
}
],
"version": 4
}
5. .vscode/settings.json
(可选)
{
"terminal.integrated.defaultProfile.windows": "Command Prompt",
"C_Cpp.default.configurationProvider": "ms-vscode.cpptools"
}
五、使用流程
操作 | 快捷键 | 效果 |
---|
编译 + 运行 | Ctrl + Shift + B | 自动清理、编译、输出到 output/ 并运行 |
启动调试 | F5 | 附加调试器调试 output/*.exe |
自定义快捷运行 | Ctrl + Alt + R (需要 keybindings) | 快速执行构建任务 |
六、测试代码样例:hello.cpp
#include <iostream>
#include <utility>
using namespace std;
int main() {
int a = 1, b = 2;
cout << "Before swap: " << a << ", " << b << "\n";
swap(a, b);
cout << "After swap: " << a << ", " << b << "\n";
return 0;
}
七、常见问题
问题 | 解决方法 |
---|
cl.exe 不是内部命令 | 没有调用 vcvars64.bat ,或 Build Tools 没安装 |
找不到 #include <iostream> | 没有设置 c_cpp_properties.json 的 includePath |
没有调试按钮(F5) | 没配置 launch.json ,或未生成 /Zi /DEBUG 信息 |
输出窗口闪退 | 脚本中未捕获 .exe 运行错误,可手动 pause |
默认 PowerShell | 设置 settings.json 绑定为 "Command Prompt" |