VSCode安装C/C++扩展:在VSCode扩展商店搜索并安装微软官方"C/C++"插件
在项目根目录创建 .vscode
文件夹,并添加以下文件:
**tasks.json(编译配置)**
{
"version": "2.0.0",
"tasks": [
{
"label": "C/C++: g++.exe build active file",
"type": "cppbuild",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": [
"$gcc"
],
"group": "build"
}
]
}
其中args
指定编译选项,如-g
生成调试信息。
**launch.json(调试配置)**:定义调试行为
{
"version": "0.2.0",
"configurations": [
{
"name": "C++ Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"cwd": "${fileDirname}",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"miDebuggerPath": "gdb",
"externalConsole": true,
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
好了,现在新建个测试文件 hello.cpp
#include <iostream>
using namespace std;
int main() {
cout << "hello, world" << endl;
system("pause");
return 0;
}
打开运行和调试视图,点击运行,会编译出 exe 文件并运行。成功。