VSCode搭建本地C编译调试环境
·
LeetCode刷题时,本地之前常用DevC++,这款IDE简洁、编译和调试运行方便,入门级完全够用。不过如果核对某个变量时,高亮功能DevC++并不支持,所以试着通过VSCode搭建本地的C编译调试环境,运行效果还可以。
Cygwin的安装
下载Cygwin安装包后,点击可执行程序:
-
安装gdb

-
安装gcc和g++

-
配置系统环境变量

-
验证是否成功
CMD窗口输入如下命令,打印出版本信息,说明安装、系统环境变量已经配成功。
gdb -v
gcc -v


VSCode配置编译和调试环境
需要先创建一个文件夹,把.cpp源码放进去。VSCode的编译和调试环境json配置,实际是从属于一个文件夹下的。
1.配置编译环境
Ctrl+Shift+P

打开tasks.json,直接拷贝如下配置即可:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "***修改为你自己的路径***\\Cygwin\\install\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-fexec-charset=UTF-8",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: ***修改为你自己的路径***\\Cygwin\\install\\bin\\g++.exe"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "***修改为你自己的路径***\\Cygwin\\install\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-fexec-charset=UTF-8",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: ***修改为你自己的路径***\\Cygwin\\install\\bin\\g++.exe"
}
]
}
2.配置调试环境
Ctrl+Shift+D
打开launch.json,进行如下修改:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug C Program",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false, // Windows 下建议设为 true 防止闪退
"MIMode": "gdb",
"miDebuggerPath": "***修改为你自己的路径***\\Cygwin\\install\\bin\\gdb.exe", // 确保 gdb 在 PATH 中
"setupCommands": [
{
"description": "Enable pretty-printing",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file" // 必须与 tasks.json 中的 label 一致
}
]
}
配置好后,点击F5,即可看到运行结果。
用大模型搜了下,Cygwin gdb对VSCode 的兼容性不太好,大模型是推荐MinGW,后续换下MinGW试下。
配置好后,不调试,右键源码仅run的结果如下:

更多推荐
所有评论(0)