以下是 Visual Studio Code (VS Code) 配置 C/C++ 开发环境的保姆级教程,适用于 Windows、Linux 和 macOS 系统:


一、安装必备组件

  1. 安装编译器

    • Windows: 安装 MinGW-w64(选择 x86_64 架构)
      • 安装后添加 bin 目录到系统环境变量 PATH(例如:C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin
    • Linux: 安装 gccg++
      sudo apt install build-essential  # Ubuntu/Debian
      

    • macOS: 安装 Xcode Command Line Tools
      xcode-select --install
      

  2. 安装 VS Code


二、配置 VS Code

  1. 安装扩展

    • 搜索并安装以下扩展:
      • C/C++ (Microsoft)
      • Code Runner (Jun Han)
  2. 验证编译器

    • 打开终端(VS Code 快捷键:Ctrl+`),输入:
      gcc --version
      g++ --version
      

    • 若显示版本信息,则安装成功。

三、创建并配置项目

  1. 新建项目文件夹

    • 创建目录(如 c_project),用 VS Code 打开此文件夹。
  2. 编写测试代码

    • 新建文件 hello.c
      #include <stdio.h>
      int main() {
          printf("Hello, C!\n");
          return 0;
      }
      

  3. 生成配置文件

    • Ctrl+Shift+P → 输入 C/C++: Edit Configurations (UI) → 选择编译器(如 gcc.exe)。
    • VS Code 会自动生成 .vscode/c_cpp_properties.json 文件(内容示例如下):
      {
          "configurations": [
              {
                  "name": "Win32",
                  "includePath": ["${workspaceFolder}/**"],
                  "defines": [],
                  "compilerPath": "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/gcc.exe",
                  "cStandard": "c17",
                  "cppStandard": "gnu++17",
                  "intelliSenseMode": "windows-gcc-x64"
              }
          ],
          "version": 4
      }
      

    • 注意:修改 compilerPath 为你的实际路径!

四、配置编译与运行

方法一:使用 Code Runner(快速运行)
  1. 配置 settings.json

    • Ctrl+, 打开设置 → 搜索 Code Runner → 勾选 Run In Terminal
    • settings.json 中添加:
      "code-runner.executorMap": {
          "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
      }
      

  2. 运行代码

    • 右键点击代码文件 → Run Code,或使用快捷键 Ctrl+Alt+N
方法二:使用 VS Code 原生调试(推荐)
  1. 配置调试任务

    • 创建 .vscode/launch.json
      • F5 → 选择 C++ (GDB/LLDB) → 选择编译器(如 gcc.exe)。
    • 修改生成的 launch.json
      {
          "version": "0.2.0",
          "configurations": [
              {
                  "name": "C/C++: gcc.exe",
                  "type": "cppdbg",
                  "request": "launch",
                  "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${workspaceFolder}",
                  "environment": [],
                  "externalConsole": false,
                  "MIMode": "gdb",
                  "miDebuggerPath": "gdb.exe",
                  "setupCommands": [...],
                  "preLaunchTask": "build"
              }
          ]
      }
      

  2. 配置编译任务

    • 创建 .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
                  }
              }
          ]
      }
      
  3. 调试运行

    • F5 启动调试,自动编译并运行。

五、常见问题解决

  1. 编译器路径错误
    • 检查 c_cpp_properties.json 中的 compilerPath 是否正确。
  2. 调试时提示“找不到文件”
    • 确保 tasks.json 中的输出路径(${fileDirname}/${fileBasenameNoExtension}.exe)与 launch.json 中的 program 一致。
  3. 中文乱码
    • tasks.jsonargs 中添加 -fexec-charset=UTF-8

六、总结

完成以上步骤后,你的 VS Code 已支持:

  • 语法高亮与智能提示
  • 一键编译运行(Code Runner)
  • 断点调试(原生调试器)

保存配置文件(c_cpp_properties.jsontasks.jsonlaunch.json),后续项目可直接复用!

更多推荐