🚀 使用 VS Code + g++ 開發 Qt GUI 項目的完整指南(Windows + MSYS2)
本指南幫助你在 Windows 下使用 VS Code + g++ + CMake + Qt6 快速搭建 Qt GUI 項目,適合熟悉 Visual Studio 的開發者向跨平臺 VS Code 工具鏈遷移。
🛠? 一、環境準備
1. 安裝 MSYS2
- 官網:https://www.msys2.org
- 初始化更新(MSYS2 MSYS 終端):
pacman -Syu
2. 安裝 Qt 和工具鏈(在 MinGW64 終端中):
pacman -S \mingw-w64-x86_64-toolchain \mingw-w64-x86_64-cmake \mingw-w64-x86_64-ninja \mingw-w64-x86_64-qt6-base \mingw-w64-x86_64-qt6-tools \mingw-w64-x86_64-qt6-declarative \mingw-w64-x86_64-qt6-svg \mingw-w64-x86_64-qt6-shadertools \mingw-w64-x86_64-qt6-multimedia \mingw-w64-x86_64-qt6-translations
📁 二、項目目錄結構
QtGuiApp/
│
├── .vscode/
│ ├── c_cpp_properties.json # IntelliSense
│ ├── tasks.json # 構建任務
│ ├── launch.json # 調試配置
│
├── build/ # 構建輸出目錄
├── CMakeLists.txt # CMake 配置
├── main.cpp # 主程序
├── mainwindow.cpp / .h / .ui # Qt 主窗口
?? 三、CMake 配置 CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(QtGuiApp LANGUAGES CXX)set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)find_package(Qt6 REQUIRED COMPONENTS Widgets)add_executable(QtGuiAppmain.cppmainwindow.cppmainwindow.hmainwindow.ui
)target_link_libraries(QtGuiApp PRIVATE Qt6::Widgets)
💡 四、VS Code 配置
1. IntelliSense .vscode/c_cpp_properties.json
{"configurations": [{"name": "Win32","includePath": ["${workspaceFolder}/**","C:/msys64/mingw64/include","C:/msys64/mingw64/include/c++/14.2.0","C:/msys64/mingw64/include/c++/14.2.0/x86_64-w64-mingw32","C:/msys64/mingw64/include/Qt6","C:/msys64/mingw64/include/Qt6/QtWidgets","C:/msys64/mingw64/include/Qt6/QtCore"],"defines": ["_DEBUG", "UNICODE"],"compilerPath": "C:/msys64/mingw64/bin/g++.exe","cStandard": "c17","cppStandard": "c++17","intelliSenseMode": "windows-gcc-x64"}],"version": 4
}
2. 構建與運行任務 .vscode/tasks.json
{"version": "2.0.0","tasks": [{"label": "CMake Configure","type": "shell","command": "cmake -S . -B build -G Ninja","group": "build"},{"label": "CMake Build","type": "shell","command": "cmake --build build","group": "build","dependsOn": ["CMake Configure"]},{"label": "Run Executable","type": "shell","command": ".\\build\\QtGuiApp.exe","group": {"kind": "test","isDefault": true},"dependsOn": ["CMake Build"]}]
}
3. 調試配置 .vscode/launch.json
{"version": "0.2.0","configurations": [{"name": "Debug Qt App","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/QtGuiApp.exe","args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}/build","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe","setupCommands": [{"description": "Enable pretty-printing","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}
📦 五、使用 windeployqt 打包 Qt GUI 應用
在 MSYS2 MinGW64 終端中,使用以下命令將 Qt 依賴復制到可執行文件目錄:
cd build
windeployqt QtGuiApp.exe
該命令會自動將 Qt DLL、平臺插件、SVG 模塊等復制至當前目錄,生成可獨立運行的
.exe
包。
🔍 六、Visual Studio 與 VS Code 使用差異對比
特性 | Visual Studio | VS Code + g++ + CMake |
---|---|---|
工程結構 | .sln , .vcxproj 圖形化工程 | CMakeLists.txt 構建系統 |
編譯器 | MSVC | g++ (MinGW64 from MSYS2) |
構建方式 | 菜單或按鈕自動構建 | tasks.json 自定義任務/命令行構建 |
調試器 | 內建 VS 調試器 | gdb + launch.json 配置調試 |
跨平臺 | Windows 為主 | 跨平臺:Windows / Linux / macOS |
UI 設計工具 | Qt Designer 插件或內嵌 | 獨立 Qt Designer(.ui 文件) |
? 附:檢查 g++ 支持的 C++ 標準版本
g++ -std=c++17 -dM -E - < nul | findstr __cplusplus