Windows11配置MSYS2+vscode+cpp+cmake环境

1 安装vscode和c++扩展


2 安装MSYS2

2.1 安装mingw工具链(内含g++等工具)

MSYS2 MSYS窗口运行

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

2.2 添加环境变量

  • 在开始菜单搜索编辑系统环境变量
  • 编辑Path,将msys64\ucrt64\bin的绝对路径添加进去

2.3 验证安装

Win+R并输入cmd打开终端,输入以下内容进行验证

bash 复制代码
gcc --version
g++ --version
gdb --version

2.4 安装cmake

MSYS2 MSYS窗口运行

bash 复制代码
pacman -S mingw-w64-ucrt-x86_64-cmake
  • 验证安装:
    Win+R并输入cmd打开终端,输入以下内容进行验证

首先检查路径对不对(一般是ucrt64\bin\cmake.exe)

bash 复制代码
where cmake

然后

bash 复制代码
cmake --version

3 vscode配置文件

c_cpp_properties.json

bash 复制代码
{
    "configurations": [
        {
            "name": "GCC",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22000.0",
            "compilerPath": "C:\\Users\\msys64\\ucrt64\\bin\\g++.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-gcc-x64"
        }
    ],
    "version": 4
}

launch.json

bash 复制代码
{
    "version": "2.0.0",
    "configurations": [
        {
            "name": "C/C++: 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:\\Users\\msys64\\ucrt64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

tasks.json

bash 复制代码
{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C:\\Users\\msys64\\ucrt64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always", // 彩色输出编译错误信息
                "-g",
                "${file}",
                "${workspaceFolder}/include/*.cpp",
                "-I",
                "${workspaceFolder}/include",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "Task generated by Debugger."
        }
    ],
    "version": "2.0.0"
}
相关推荐
凉茶钱1 天前
【c语言】动态内存管理:malloc,calloc,realloc,柔性数组
c语言·c++·vscode·柔性数组
hele_two1 天前
VS Code + CMake 调用 SDL2 & SDL2_image 完整编译教程(Windows 平台)
c++·windows·vscode·图形渲染
secondyoung1 天前
Markdown数学公式语法速查手册
算法·编辑器·markdown·latex
wjhx1 天前
关于xcode中使用图标
ide·macos·xcode
web行路人1 天前
Claude code在Vscode编辑器中使用整理
编辑器
漫随流水2 天前
创建一个IDEA的Java项目
java·ide·intellij-idea
bigcarp2 天前
IDE中AI辅助编程时禁止AI读取指定的文件或文件夹
vscode
且去填词2 天前
VSCode 中使用 Codex:命令、Agent 与 Skills 完整指南
ide·人工智能·vscode·编辑器·codex
PersistJiao2 天前
开发环境对比:VS Code、Cursor、IntelliJ IDEA
java·ide·intellij-idea
weixin_452077642 天前
VS code 使用STM32CubelDE for Visual Studio Code环境,如何配置CMakeLists.txt新增其他.C文件路径
c语言·vscode·stm32