You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows端运行Flutter项目遭遇C++编译错误求助

Flutter Windows桌面编译错误解决办法

新建Flutter项目后,Windows端运行时抛出以下编译错误:

C:\Users\muham\StudioProjects\untitled\windows\runner\utils.cpp(54,43): error C2220:  warning treated as error - no 'object' file generated [C:\Users\muham\StudioProjects\untitled\build\windows\runner\untitled.vcxproj]
C:\Users\muham\StudioProjects\untitled\windows\runner\utils.cpp(54,43): warning C4018:  '>': signed/unsigned mismatch [C:\Users\muham\StudioProjects\untitled\build\windows\runner\untitled.vcxproj]
Exception: Build process failed.

已尝试的无效操作:

  • 修改CMakeLists.txt中的target_compile_options(${TARGET} PRIVATE /W4 /WX /wd"4100"),删除/WX或改为/WX-,无效
  • 将utils.cpp中代码从
    if (target_length <= 0 || target_length > utf8_string.max_size()) {
    
    修改为
    if (target_length <= 0 || static_cast<size_t>(target_length) > utf8_string.max_size()) {
    
    仍无效

可行解决步骤

1. 清理构建缓存

先彻底清除旧构建文件,避免缓存干扰:

flutter clean

Windows PowerShell可额外执行:

Remove-Item -Recurse -Force build/ windows/build/

2. 正确修改CMakeLists.txt

找到项目根目录下的windows/CMakeLists.txt,定位到target_compile_options行,确保修改为:

target_compile_options(${TARGET} PRIVATE /W4 /WX- /wd"4100")

/WX-表示禁用「警告视为错误」规则,修改后保存文件。

3. 确认utils.cpp修改生效

确保修改的是**项目根目录下windows/runner/utils.cpp**的代码,修改后保存。

4. 检查Visual Studio环境

  • 确认安装Visual Studio 2019/2022,且勾选「Desktop development with C++」组件
  • 检查是否安装对应版本的Windows SDK,可通过flutter doctor查看环境提示

完成以上步骤后,重新执行:

flutter run -d windows

补充:用户运行的是默认Flutter模板代码,无逻辑问题。

内容的提问来源于stack exchange,提问作者mohammad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:55:21