首先安装MinGW-w64并配置环境变量,再在VSCode中安装C/C++扩展,接着创建C文件并编写代码,然后配置tasks.json和launch.json指定编译器和调试器路径,最后通过Ctrl+Shift+B编译、F5调试运行;常见报错多因路径配置错误或环境未正确设置。

在VSCode中运行C代码,需要配置C语言环境,包括安装编译器、配置环境变量,以及在VSCode中安装必要的插件。简单来说,就是让VSCode“认识”C语言,并能顺利地编译和运行你的代码。
安装C/C++编译器 (MinGW)
安装VSCode及C/C++扩展
创建C代码文件
立即学习“C语言免费学习笔记(深入)”;
.c
hello.c
#include <stdio.h>
int main() {
printf("Hello, VSCode!
");
return 0;
}配置tasks.json
tasks.json
Ctrl+Shift+P
Cmd+Shift+P
tasks.json
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\mingw-w64\mingw64\bin\gcc.exe", // 修改为你的gcc路径
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}配置launch.json
launch.json
launch.json
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:\mingw-w64\mingw64\bin\gdb.exe", // 修改为你的gdb路径
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}编译和运行代码
Ctrl+Shift+B
Cmd+Shift+B
F5
.exe
常见原因包括:
bin
Path
tasks.json
launch.json
一个容易忽略的点是,如果你的代码依赖于某些库,需要在编译时链接这些库。比如,使用了数学库,需要在
tasks.json
args
-lm
调试C代码需要正确配置
launch.json
miDebuggerPath
gdb.exe
F5
另外,如果程序需要从控制台输入,确保
launch.json
externalConsole
true
当然。除了MinGW,你还可以选择:
选择哪个编译器取决于你的个人偏好和项目需求。MinGW是一个不错的选择,因为它易于安装和配置,并且是免费的。MSVC和Clang则提供了更多的功能和更好的性能。
以上就是VSCode怎么运行C代码_VSCode配置C语言环境与编译运行教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号