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"
}
相关推荐
渐暖°4 小时前
【leetcode算法从入门到精通】5. 最长回文子串
vscode·算法·leetcode
WarmSword8 小时前
mac上用cursor/vscode调试root权限进程
c++·ide·vscode·macos·mac
HAPPY酷9 小时前
构建即自由:一份为创造者设计的 Windows C++ 自动化构建指南
开发语言·c++·ide·windows·python·策略模式·visual studio
山峰哥10 小时前
SQL优化全解析:从索引策略到查询性能飞跃
大数据·数据库·sql·编辑器·深度优先
木斯佳10 小时前
OpenTiny:快速搭建实时协作文本编辑器:基于 TinyEditor 的实践指南
编辑器
取个鸣字真的难11 小时前
简单快速的用 Claude Code 帮你创建 PPT 生成 Skills
vscode·powerpoint·ai编程
K·Herbert11 小时前
OpenClaw 私人电脑部署风险
人工智能·安全·编辑器
henysugar11 小时前
Android studio编译aidl若干问题记录
android·ide·android studio·aidl
阿斌_bingyu70911 小时前
FastAdmin 混合式语言包添加繁体中文(zh-tw)完整教程
android·ide
何中应12 小时前
PyCharm报`Invalid Python SDK`错误
ide·python·pycharm