下载g++/gcc

windows

windows下载MinGW-w64(已经包含g++)

pacman -S mingw-w64-ucrt-x86_64-gcc

之后把gcc的bin文件夹添加到系统PATH

之后验证

g++ --version

ubuntu

sudo apt install g++

配置.vscode文件夹

tasks.json

tasks.json文件是 VS Code 用于配置 C++ 编译任务的配置文件,它定义了如何用 g++ 编译你的代码

{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe",
"command": "g++",
"args": [
"-g", // 生成调试信息
"${file}", // 当前文件(如 main.cpp)
"-o", // 指定输出文件
"${fileDirname}\\${fileBasenameNoExtension}.exe" // 输出路径(如 main.exe)
],
"options": {
"cwd": "${workspaceFolder}" // 工作目录为项目根目录
},
"problemMatcher": ["$gcc"], // 用 GCC 解析错误
"group": {
"kind": "build", // 任务类型为“生成”
"isDefault": true // 默认任务(Ctrl+Shift+B)
}
}
]
}

launch.json

launch.json文件是 VS Code 用于配置 C++ 调试器的文件,它定义了如何启动调试会话(如使用 GDB 调试 C++ 程序)

{
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe", // 调试的可执行文件
"args": [], // 程序参数
"stopAtEntry": false, // 不在入口暂停
"cwd": "${workspaceFolder}", // 工作目录
"environment": [], // 环境变量
"externalConsole": false, // 使用内置终端
"MIMode": "gdb", // 调试器类型
"miDebuggerPath": "gdb", // 调试器路径
"setupCommands": [
{ "description": "启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }
],
"preLaunchTask": "C/C++: g++.exe" // 调试前先编译
}
]
}

c_cpp_properties.json

c_cpp_properties.json是 VS Code 的 C/C++ 扩展(Microsoft C/C++ 插件)的配置文件,用于控制 代码智能提示(IntelliSense)、头文件路径、编译器设置 等。它的作用和tasks.jsonlaunch.json不同,是专门为编辑体验服务的

{
"configurations": [
{
"name": "Linux", // 配置名称
"includePath": [ // 头文件搜索路径
"${workspaceFolder}/**" // 递归包含工作目录下所有文件
],
"defines": [], // 预定义宏(如 -DDEBUG)
"compilerPath": "/usr/bin/gcc", // 编译器路径
"cStandard": "c17", // C 语言标准
"cppStandard": "gnu++17", // C++ 语言标准
"intelliSenseMode": "linux-gcc-x64" // IntelliSense 模式
}
],
"version": 4 // 固定版本号
}