vscode 写个 c 程序

本文详细介绍了如何在VSCode中配置C/C++开发环境,包括安装编译器、配置环境变量、设置智能提示、调试及代码浏览,以及如何通过tasks.json和launch.json进行编译和调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装编译器

  1. 解压 mingw64 到想要的目录,如 D:\c\win\mingw64\bin
  2. 添加 D:\c\win\mingw64\bin 到环境变量

写个 c 程序

程序源码

#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i = 0;
    for(i = 0; i < 10; i++){
        printf("hello world == %d\n",i);
    }
}

用到插件c/c++

  1. 安装, ctrl+shift+x 搜索 c/c++ 并安装,支持智能提示\调试\代码浏览
  2. 配置, ctrl+shif+p 搜索 c/c++ 选择 Edit Configurations(JSON) 并编辑,如下

c_pp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "D:/C/win/mingw64/x86_64-w64-mingw32/include/**" //头文件搜索路径,**表示递归
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64",                        //配合编译器
            "compilerPath": "D:/C/win/mingw64/bin/gcc"            //编译器路径名
        }
    ],
    "version": 4
}

编译

  • 直接用命令编译, gcc -g -o helloworld.exe helloworld.c
  • 配置task来编译, ctrl+shif+p 搜索 tasks 选择 Configure Default Build Task 并编辑,如下

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "gcc",       //gcc
            "args": [               //参数
                "-g",
                "-o",
                "helloworld.exe",
                "helloworld.c"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$msCompile"
            ]
        }
    ]
}

ctrl + shift + b 编译

调试

  1. 配置,按 f5, 选c++(gdb/lldb), 选Default Configure,生成默认配置文件
  2. 执行调试,设置断点,按f5

launch.json

{
    "configurations": [
        {
            "name": "(gdb) Launch",                             //cmd,powershell lanch
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/helloworld.exe",     //要调试的程序的路径
            "args": [],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",                        //要调试的程序的目录
            "environment": [],
            "externalConsole": false,                           //使集成终端,容易观察调试输出
            "MIMode": "gdb",
            "miDebuggerPath": "C:/win/gnu/mingw64/bin/gdb.exe",             //编译器路径
            // "miDebuggerPath": "C:\\win\\gnu\\mingw64\\bin\\gdb.exe",     //ok
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

因为 vscode 会将上面的调试配置转成 cmd 格式的命令(只能在 powershell/cmd 中运行),可以通过菜单help->open process explorer 查看,如下

"C:\Users\sfuwe\.vscode\extensions\ms-vscode.cpptools-0.23.0-insiders\debugAdapters\bin\WindowsDebugLauncher.exe"
--stdin=Microsoft-MIEngine-In-oj45wsvs.dcm --stdout=Microsoft-MIEngine-Out-axvtgnnf.053 --stderr=Microsoft-MIEngine-Error-whwum2ps.z5j
--pid=Microsoft-MIEngine-Pid-naoc4b3s.mka --dbgExe=D:/C/win/mingw64/bin/gdb.exe --interpreter=mi

而且 "externalConsole": false 表示要使用 vscode 集成的终端,所以这里集成终端必须选 window 系的 powershell 或者 cmd 不能是 bash

ctrl + ,编辑设置文件,如下
settings.json

    "terminal.integrated.shell.windows":
        "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", //ok
        //"C:\\Windows\\System32\\cmd.exe"          //ok
        //"C:\\Windows\\System32\\bash.exe"             //no!!!

参考

https://code.visualstudio.com/docs

<think>我们将详细介绍如何使用VSCodeC语言程序的具体步骤,包括环境配置、编代码、编译和调试。 注意:这里假设你使用的是Windows系统,但也会提供macOS和Linux的补充说明。 步骤分为以下几个部分: 1. 安装必要的软件(MinGW编译器、VSCode) 2.VSCode中安装C/C++扩展 3. 配置编译器路径 4. 创建和编C程序 5. 编译和运行程序 6. 调试C程序 下面我们逐步进行。</think>### 使用 VSCode C 语言程序详细步骤 #### 完整环境配置流程 ```markdown 1. **安装必要软件** - [VSCode 官方下载](https://code.visualstudio.com/) - [MinGW-w64 编译器](https://sourceforge.net/projects/mingw-w64/)(Windows) - macOS/Linux: 使用系统自带终端安装 ```bash # macOS brew install gcc # Ubuntu/Debian sudo apt install build-essential ``` 2. **配置编译器路径(Windows)** - 安装 MinGW 时勾选: - `x86_64` 架构 - `posix` 线程模型 - `seh` 异常处理 - 添加环境变量: ```powershell # 系统属性 → 高级 → 环境变量 → Path → 添加 C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin ``` - 验证安装: ```cmd gcc --version ``` 3. **VSCode 扩展安装** - 必需扩展: - C/C++ (Microsoft) - C/C++ Extension Pack - Code Runner (可选) - 安装方法:扩展面板 → 搜索安装 → 重启 VSCode 4. **创建工作区** ```bash mkdir c_projects && cd c_projects code . ``` ``` #### 创建并运行第一个程序 1. **创建源文件** - 在资源管理器右键 → `新建文件` → `hello.c` - 输入以下代码: ```c #include <stdio.h> int main() { printf("Hello, VSCode C World!\n"); return 0; } ``` 2. **配置构建任务** - `Ctrl+Shift+P` → 输入 `Tasks: Configure Task` → 选择 `C/C++: gcc.exe` - 自动生成 `tasks.json`: ```json { "version": "2.0.0", "tasks": [ { "type": "cppbuild", "label": "C/C++: gcc.exe build active file", "command": "gcc", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": ["$gcc"], "group": { "kind": "build", "isDefault": true } } ] } ``` 3. **编译运行** - 编译:`Ctrl+Shift+B` → 选择构建任务 - 运行: - 方法1:终端执行 `.\hello.exe` - 方法2:安装 Code Runner 扩展 → 点击右上角 ▷ 图标 #### 调试配置 1. **创建调试配置** - 切换到调试视图 → 创建 `launch.json` - 选择 `C++ (GDB/LLDB)` - 自动生成配置: ```json { "version": "0.2.0", "configurations": [ { "name": "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": "gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ], "preLaunchTask": "C/C++: gcc.exe build active file" } ] } ``` 2. **调试操作** - 设置断点:行号左侧点击 - 启动调试:`F5` - 调试控制: | 快捷键 | 功能 | |--------|--------------| | F5 | 开始/继续 | | F10 | 单步跳过 | | F11 | 单步进入 | | Shift+F11 | 单步跳出 | | F9 | 切换断点 | #### 推荐工作流优化 ```jsonc // settings.json 优化配置 { "files.associations": { "*.c": "c" }, "C_Cpp.intelliSenseEngine": "Default", "editor.formatOnSave": true, "code-runner.runInTerminal": true, "code-runner.saveFileBeforeRun": true } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值