Windows 11下无法构建Flutter桌面应用,寻求解决方法
Flutter Windows桌面项目编译报错:No CMAKE_CXX_COMPILER could be found
问题详情
在Android Studio中选择Windows(桌面)选项运行Flutter项目时,出现编译错误:
Launching lib\main.dart on Windows in debug mode...
Building Windows application...
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_CXX_COMPILER could be found.Exception: Unable to generate build files
已执行操作:
- 安装Visual Studio Code及开发工具、Windows SDK
- 通过Android Studio SDK管理器安装CMake
Flutter Doctor检测结果:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.3.2, on Microsoft Windows [Version 10.0.22622.598], locale en-US) [√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) [√] Chrome - develop for the web [√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.3.5) [√] Android Studio (version 2021.2) [√] IntelliJ IDEA Ultimate Edition (version 2021.3) [√] VS Code (version 1.69.1) [√] Connected device (3 available) ! Device emulator-5554 is offline. ! Device emulator-5562 is offline. [√] HTTP Host Availability
解决方法
1. 补全Visual Studio C++开发组件
Flutter Doctor仅检测Visual Studio是否安装,但可能缺少C++桌面开发的核心组件:
- 打开Visual Studio Installer,选择“修改”当前的Visual Studio Community 2022
- 在“工作负载”标签下勾选**“使用C++的桌面开发”**,确保包含以下组件:
- MSVC v143 - VS 2022 C++ x64/x86生成工具(对应你的VS版本)
- 与系统匹配的Windows SDK(如Windows 11 SDK)
- C++ CMake工具 for Windows
- 完成安装后重启电脑
2. 配置CMake环境变量
确保CMake可执行文件路径加入系统环境变量:
- 找到Android Studio安装的CMake路径,示例:
C:\Users\<你的用户名>\AppData\Local\Android\Sdk\cmake\3.22.1\bin - 右键“此电脑”→“属性”→“高级系统设置”→“环境变量”
- 在“系统变量”的
Path中添加上述CMake bin目录 - 重启Android Studio或终端,使环境变量生效
3. 手动指定C++编译器到CMakeLists.txt
若上述方法无效,可在项目的Windows配置文件中手动指定编译器:
打开项目根目录下的windows/CMakeLists.txt,在project命令前添加:
set(CMAKE_CXX_COMPILER "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe")
注意替换路径为你实际的Visual Studio安装路径及MSVC版本号
4. 清理并重新构建
在Android Studio终端执行以下命令:
flutter clean flutter pub get flutter run -d windows
内容的提问来源于stack exchange,提问作者Jiehfeng
相关产品推荐
相关产品推荐

