要在 Visual Studio Code (VSCode) 中配置 C 语言编程环境,你需要遵循以下步骤:
首先确保已经安装了 Visual Studio Code。如果还没有安装,可以从 VSCode 官网下载并安装。
Ctrl+Shift+X
),搜索并安装由 Microsoft 提供的 C/C++ 扩展。brew install gcc
sudo apt update
sudo apt install build-essential
在你的项目中创建一个 tasks.json
文件来配置编译任务。路径为 .vscode/tasks.json
。示例配置如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": ["$gcc"],
"detail": "Generated task for building C file"
}
]
}
创建一个 launch.json
来配置调试任务。路径为 .vscode/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",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build",
"miDebuggerPath": "gdb",
"logging": {
"trace": true,
"traceResponse": true,
"engineLogging": true
}
}
]
}
.c
文件并写入 C 代码。Ctrl+Shift+B
进行编译。F5
启动调试,会自动编译并运行你的程序。通过这些步骤,你的 VSCode 应该已经配置好了一个基本的 C 语言开发环境。请根据具体的操作系统和需求调整编译器路径和其他配置。