首先安装MinGW并配置环境变量,确保gcc可用;接着安装VSCode及C/C++扩展;然后配置tasks.json实现编译任务,指定gcc编译器与输出路径;再配置launch.json设置调试器gdb路径,确保miDebuggerPath正确;最后通过F5启动调试,解决常见报错需检查路径、编码与GDB兼容性;中文乱码问题可通过设置chcp 65001和UTF-8编码解决;使用CMake时需创建CMakeLists.txt并借助CMake Tools扩展管理构建。

无需多言,VSCode 绝对能成为你调试 C 语言程序的利器。搭建 C 开发环境可能略有挑战,但绝对值得。
首先,你需要安装 MinGW,然后配置 VSCode 的 tasks.json 和 launch.json 文件。
解决方案
安装 MinGW (Minimalist GNU for Windows):
立即学习“C语言免费学习笔记(深入)”;
posix
seh
bin
Path
C:mingw64in
gcc -v
安装 VSCode 及 C/C++ 扩展:
配置 tasks.json
Ctrl+Shift+P
Cmd+Shift+P
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: gcc.exe"
}
]
}command
args
-g
${file}-o
problemMatcher
配置 launch.json
Ctrl+Shift+D
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++: gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\mingw64\bin\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}program
miDebuggerPath
externalConsole
false
true
开始调试:
F5
常见错误包括:
bin
Path
tasks.json
launch.json
miDebuggerPath
VSCode 的 C/C++ 扩展集成了 GDB 调试器。
launch.json
miDebuggerPath
F5
GDB 调试器允许你:
中文乱码通常是由于编码不一致引起的。
确保 C 源文件使用 UTF-8 编码保存。
在 launch.json
setupCommands
"setupCommands": [
{
"description": "Set GDB encoding to UTF-8",
"text": "-exec shell chcp 65001"
}
]在 VSCode 的设置中,将终端的编码设置为 UTF-8。 (File -> Preferences -> Settings, 搜索 "terminal.integrated.encoding", 设置为 "utf-8")
如果使用外部控制台,确保控制台支持 UTF-8 编码。
CMake 是一个跨平台的构建系统,可以简化 C 项目的构建过程。
安装 CMake。
安装 VSCode 的 CMake Tools 扩展。
在项目根目录下创建一个 CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0) project(MyProject) add_executable(MyProject main.c)
使用 CMake Tools 扩展配置和构建项目。 (点击 VSCode 底部状态栏的 "CMake: [未配置]",选择一个编译器,然后点击 "Build")
配置 launch.json
使用 CMake 可以更灵活地管理项目依赖、构建选项等。
以上就是VSCode调试C语言程序 完整VSCode搭建C开发环境教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号