WIN11使用vscode搭建c语言开发环境

安装 VS Code
  • 下载地址: Visual Studio Code - Code Editing. Redefined

  • 安装时勾选 "添加到 PATH" (方便在终端中调用 code 命令

    下载 MSYS2

  • 官网:MSYS2

  • 下载 msys2-x86_64-xxxx.exe(64位版本)并安装。

  • 默认安装路径:C:\msys64

  • 运行 MSYS2 终端

    • 安装完成后,打开 MSYS2 MSYS(开始菜单或桌面快捷方式)。
  • 更新软件包数据库

    • 在 MSYS2 终端运行

      复制代码
      pacman -Syu
      如果提示关闭终端,重新打开 MSYS2 并再次运行:
      复制代码
      pacman -Su

安装 MinGW-w64 GCC(G++)

MSYS2 提供了多个版本的 GCC:

  • UCRT64(推荐,兼容性更好)

  • MINGW64(传统 MinGW-w64)

  • CLANG64(LLVM Clang 版本)

安装 UCRT64 版本的 GCC(推荐)

复制代码
pacman -S --needed base-devel mingw-w64-ucrt-x86_64-toolchain
  • 按回车选择默认安装(全部包)。

方法 2:安装 MINGW64 版本的 GCC(传统 MinGW-w64)

复制代码
pacman -S --needed base-devel mingw-w64-x86_64-toolchain

方法 3:安装 CLANG64 版本的 GCC(LLVM Clang)

复制代码
pacman -S --needed base-devel mingw-w64-clang-x86_64-toolchain

将 MinGW-w64 添加到系统环境变量

安装完成后,GCC 的可执行文件(gccg++gdb)位于:

  • UCRT64 : C:\msys64\ucrt64\bin

  • MINGW64 : C:\msys64\mingw64\bin

  • CLANG64 : C:\msys64\clang64\bin

添加环境变量
  1. 打开系统环境变量设置

    • Win + S 搜索 "编辑系统环境变量""环境变量"
  2. 修改 PATH

    • "系统变量" 中找到 Path,点击 "编辑""新建"

    • 添加你的 MinGW-w64 的 bin 目录(例如 C:\msys64\ucrt64\bin

修改 c_cpp_properties.json
  1. 在 VS Code 中打开命令面板(Ctrl+Shift+P),输入 C/C++: Edit Configurations (UI)

  2. 设置:

    • Compiler path : C:\msys64\ucrt64\bin\g++.exe(根据你的安装路径调整)。

    • IntelliSense mode : gcc-x64

修改 tasks.json(编译配置)

Ctrl+Shift+PTasks: Configure TaskC/C++: g++.exe build active file ,修改 args

json

复制

下载

复制代码
{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "cppbuild",
      "label": "C/C++: g++.exe build active file",
      "command": "C:\\msys64\\ucrt64\\bin\\g++.exe",
      "args": [
        "-fdiagnostics-color=always",
        "-g",
        "${file}",
        "-o",
        "${fileDirname}\\${fileBasenameNoExtension}.exe",
        "-std=c++17"  // 可选:指定 C++ 标准
      ],
      "options": {
        "cwd": "${fileDirname}"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "detail": "Generated task by VS Code"
    }
  ]
}
修改 launch.json(调试配置)

Ctrl+Shift+Dcreate a launch.json fileC++ (GDB/LLDB),修改:

json

复制

下载

复制代码
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "g++.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:\\msys64\\ucrt64\\bin\\gdb.exe",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "C/C++: g++.exe build active file"
    }
  ]
}

测试运行

  1. 创建一个 hello.cpp 文件:

    cpp

    复制

    下载

    复制代码
    #include <iostream>
    using namespace std;
    int main() {
        cout << "Hello, MSYS2 GCC!" << endl;
        return 0;
    }
  2. 编译运行

    • Ctrl+Shift+B 编译。

    • F5 调试运行。

相关推荐
学嵌入式的小杨同学5 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
小魏小魏我们去那里呀7 小时前
Alibaba Cloud DevOps Integration For JetBrains 插件使用指南
ide·阿里云·devops·jetbrains
badfl8 小时前
VSCode Claude Code插件配置教程:使用、配置中转API、常见问题
人工智能·vscode·ai
aidou131414 小时前
Visual Studio Code(VS Code)安装步骤
vscode·npm·node.js·环境变量
学嵌入式的小杨同学16 小时前
【Linux 封神之路】进程进阶实战:fork/vfork/exec 函数族 + 作业实现(含僵尸进程解决方案)
linux·开发语言·vscode·嵌入式硬件·vim·软件工程·ux
Laurence16 小时前
从零到一构建 C++ 项目(IDE / 命令行双轨实现)
前端·c++·ide
我待_JAVA_如初恋19 小时前
安装idea教程
java·ide·intellij-idea
mftang19 小时前
STM32Cube IDE 详细介绍
ide·stm32·嵌入式硬件
綦枫Maple19 小时前
IDEA选择“在当前窗口打开”还是“新窗口打开”的提示不见了,如何恢复?
java·ide·intellij-idea
一路向北North19 小时前
vscode 安装插件非常慢
ide·vscode·编辑器