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"
}
相关推荐
三品吉他手会点灯2 小时前
STM32 VSCode 开发-C/C++的环境配置中,找不到C/C++: Edit Configurations选项
c语言·c++·vscode·stm32·单片机·嵌入式硬件·编辑器
小堃学编程6 小时前
【项目实战】基于protobuf的发布订阅式消息队列(4)—— 服务端
c语言·c++·vscode·消息队列·gtest·protobuf·muduo
2501_915909069 小时前
Xcode从入门到精通:全面解析iOS开发IDE的核心功能与实际应用指南
ide·vscode·ios·个人开发·xcode·swift·敏捷流程
Cephas、10 小时前
VsCode 使用手册
vscode·工具使用手册
景庆19710 小时前
vscode启动springBoot项目配置,激活环境
java·开发语言·vscode
纪伊路上盛名在10 小时前
如何跨设备访问我们VSCode中GitHub Copilot 的聊天记录?
vscode·github·copilot·工作流
青花瓷10 小时前
PyCharm中创建Django基础网站
ide·pycharm·django
小王C语言10 小时前
vscode连接云服务器
ide·vscode·编辑器
打小就很皮...10 小时前
Trae 和 VSCode 中 Claude Code 插件使用
ide·vscode·编辑器·claude
m0_6348654010 小时前
idea快捷建项目
java·ide·intellij-idea