如何在VSCode中更换gcc编译器与gdb调试器?
在Windows 11的VSCode中使用自定义gcc/gdb/g++工具链
确认工具链路径
找到你安装的gcc、gdb、g++所在的bin目录,比如D:\CustomToolchain\bin,务必确保这个目录下存在gcc.exe、g++.exe、gdb.exe这三个核心可执行文件。配置VSCode全局设置
- 按
Ctrl+,打开设置界面,点击右上角的{}图标进入settings.json编辑模式 - 添加以下配置(把路径替换成你的工具链
bin目录):"terminal.integrated.env.windows": { "PATH": "D:\\CustomToolchain\\bin;${env:PATH}" }, "C_Cpp.default.compilerPath": "D:\\CustomToolchain\\bin\\g++.exe", "C_Cpp.default.debuggerPath": "D:\\CustomToolchain\\bin\\gdb.exe" - 保存文件,这会让VSCode默认优先调用你指定的工具链
- 按
配置编译任务(tasks.json)
- 打开你的C/C++项目,按
Ctrl+Shift+B,选择「创建tasks.json文件」,再选「Others」模板 - 修改为如下内容(替换路径和编译参数):
{ "version": "2.0.0", "tasks": [ { "label": "Compile with custom GCC", "type": "shell", "command": "D:\\CustomToolchain\\bin\\g++.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] } - 保存后,按
Ctrl+Shift+B就能用自定义工具链编译当前文件了
- 打开你的C/C++项目,按
配置调试环境(launch.json)
- 点击左侧「运行和调试」图标(快捷键
Ctrl+Shift+D),选择「创建launch.json文件」,再选「C++ (GDB/LLDB)」模板 - 修改为如下内容:
{ "version": "0.2.0", "configurations": [ { "name": "Debug with custom GDB", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "D:\\CustomToolchain\\bin\\gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "Compile with custom GCC" } ] } - 保存后,按
F5即可启动调试,使用你指定的gdb工具
- 点击左侧「运行和调试」图标(快捷键
验证工具链是否生效
- 打开VSCode终端(快捷键
Ctrl+),输入gcc --version和gdb --version,检查输出的版本是否为你安装的自定义工具链版本 - 编写一个简单的C++测试程序,编译并调试,确认整个流程正常运行
- 打开VSCode终端(快捷键
内容的提问来源于stack exchange,提问作者Diego
相关产品推荐
相关产品推荐

