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

使用C++和CMake构建Qt Quick应用时遇编译错误求助

问题:Qt Quick应用CMake生成构建文件成功,但编译时出现解析错误

顶层CMake文件

cmake_minimum_required(VERSION 3.28)

project(CGPA_Calculator VERSION 0.1 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)


set(CMAKE_PREFIX_PATH "D:/Dev_Install/Qt/6.6.1/mingw_64" CACHE PATH "Qt6 install path")
set(Qt6_DIR "D:/Dev_Install/Qt/6.6.1/mingw_64/lib/cmake/Qt6" CACHE PATH "Qt6 cmake directory")

find_package(Qt6 6.1 REQUIRED COMPONENTS Quick)

qt_standard_project_setup()
add_subdirectory(src)
add_subdirectory(QML_Files)
qt_add_qml_module(app
    URI Calc
    VERSION 1.0
    QML_FILES
        QML_Files/Main.qml
        QML_Files/Page1.qml
        QML_Files/Body.qml
        QML_Files/ContHead.qml
    #    SOURCES
    #src/cgpa_calculator.cpp src/cgpa_calculator.h
)


set_target_properties(app PROPERTIES
    WIN32_EXECUTABLE TRUE
)


target_link_libraries(app PRIVATE Qt6::Quick)

include(GNUInstallDirs)
install(TARGETS app
    BUNDLE DESTINATION ${CMAKE_INSTALL_BINDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )

环境信息

  • 操作系统:Windows 11
  • Qt版本:6.6.1
  • 编译器:MSYS2的mingw64 g++
  • 命令行工具:Powershell

目录结构

PS O:\CGPA Calculator> tree .
Folder PATH listing for volume Coding
Volume serial number is 6CE9-894C
O:\CGPA CALCULATOR
├───build
│   ├───.qt
│   │   └───bin
│   ├───.rcc
│   ├───Calc
│   │   └───QML_Files
│   ├───CMakeFiles
│   │   ├───3.28.1
│   │   │   └───CompilerIdCXX
│   │   │       └───tmp
│   │   ├───all_qmllint.dir
│   │   ├───all_qmllint_json.dir
│   │   ├───all_qmllint_module.dir
│   │   ├───all_qmltyperegistrations.dir
│   │   ├───app_app_raw_qml_0.dir
│   │   ├───app_automoc_json_extraction.dir
│   │   ├───app_other_files.dir
│   │   ├───app_qmake_Calc.dir
│   │   ├───app_qmlcachegen.dir
│   │   ├───app_qmllint.dir
│   │   ├───app_qmllint_json.dir
│   │   ├───app_qmllint_module.dir
│   │   ├───app_qmltyperegistration.dir
│   │   ├───app_tooling.dir
│   │   ├───CMakeScratch
│   │   ├───pkgRedirects
│   │   └───Progress
│   ├───QML_Files
│   │   └───CMakeFiles
│   └───src
│       ├───.rcc
│       │   └───qmlcache
│       ├───CMakeFiles
│       │   ├───app.dir
│       │   │   ├───.rcc
│       │   │   │   └───qmlcache
│       │   │   ├───app_autogen
│       │   │   └───__
│       │   │       └───.rcc
│       │   │           └───qmlcache
│       │   │               └───app_QML_Files
│       │   ├───app_autogen.dir
│       │   └───app_autogen_timestamp_deps.dir
│       ├───meta_types
│       └───qmltypes
├───CMakeExtras
│   ├───.qt
│   │   └───bin
│   ├───.rcc
│   ├───Calc
│   │   └───QML_Files
│   ├───CMakeFiles
│   │   ├───3.28.1
│   │   │   └───CompilerIdCXX
│   │   │       └───tmp
│   │   ├───all_qmllint.dir
│   │   ├───all_qmllint_json.dir
│   │   ├───all_qmllint_module.dir
│   │   ├───all_qmltyperegistrations.dir
│   │   ├───app_app_raw_qml_0.dir
│   │   ├───app_automoc_json_extraction.dir
│   │   ├───app_other_files.dir
│   │   ├───app_qmake_Calc.dir
│   │   ├───app_qmlcachegen.dir
│   │   ├───app_qmllint.dir
│   │   ├───app_qmllint_json.dir
│   │   ├───app_qmllint_module.dir
│   │   ├───app_qmltyperegistration.dir
│   │   ├───app_tooling.dir
│   │   ├───CMakeScratch
│   │   └───pkgRedirects
│   ├───QML_Files
│   │   └───CMakeFiles
│   └───src
│       ├───.rcc
│       │   └───qmlcache
│       ├───CMakeFiles
│       │   ├───app.dir
│       │   │   ├───.rcc
│       │   │   │   └───qmlcache
│       │   │   ├───app_autogen
│       │   │   └───__
│       │   │       └───.rcc
│       │   │           └───qmlcache
│       │   │               └───app_QML_Files
│       │   ├───app_autogen.dir
│       │   └───app_autogen_timestamp_deps.dir
│       ├───meta_types
│       └───qmltypes
├───QML_Files
└───src

构建文件生成命令及输出

执行命令:

cmake -G "MinGW Makefiles"  -S . -B ".\build"

输出:

-- The CXX compiler identification is GNU 13.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Performing Test HAVE_STDATOMIC
-- Performing Test HAVE_STDATOMIC - Success
-- Found WrapAtomic: TRUE
-- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR)
-- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR)
CMake Warning (dev) at D:/Dev_Install/Qt/6.6.1/mingw_64/lib/cmake/Qt6Core/Qt6CoreMacros.cmake:2768 (message):
  Qt policy QTP0001 is not set: ':/qt/qml/' is the default resource prefix
  for QML modules.  Check https://doc.qt.io/qt-6/qt-cmake-policy-qtp0001.html
  for policy details.  Use the qt_policy command to set the policy and
  suppress this warning.

Call Stack (most recent call first):
  D:/Dev_Install/Qt/6.6.1/mingw_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:468 (__qt_internal_setup_policy)
  D:/Dev_Install/Qt/6.6.1/mingw_64/lib/cmake/Qt6Qml/Qt6QmlMacros.cmake:716 (qt6_add_qml_module)
  CMakeLists.txt:17 (qt_add_qml_module)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done (2.7s)
-- Generating done (0.1s)
-- Build files have been written to: O:/CGPA Calculator/build

编译错误信息

执行命令:

cmake --build .\build

错误输出:

[  4%] Automatic QML type registration for target app
Error 5 while parsing O:/CGPA Calculator/build/src/meta_types/qt6app_metatypes.json: illegal value
mingw32-make[2]: *** [CMakeFiles\app_tooling.dir\build.make:135: src/app_qmltyperegistrations.cpp] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:444: CMakeFiles/app_tooling.dir/all] Error 2
mingw32-make: *** [Makefile:135: all] Error 2

备注

  • 运行qml .\QML_Files\Main.qml可正常启动QML文件
  • 仅用C++构建应用,未关联其他逻辑

解决方案

问题根源

这个错误是QML类型注册过程中生成的JSON文件格式异常导致的,常见触发原因包括:

  1. CMake中add_subdirectory与qt_add_qml_module的QML路径重复处理,引发资源冲突
  2. 未设置Qt策略QTP0001,导致资源路径处理不规范
  3. 项目路径包含空格,Windows下解析路径时出现异常

修复步骤

  1. 移除冗余的子目录添加
    因为已经在qt_add_qml_module中指定了QML文件路径,无需再添加子目录构建,修改CMakeLists.txt:

    # add_subdirectory(src)
    # add_subdirectory(QML_Files)
    
  2. 设置Qt策略QTP0001
    在qt_standard_project_setup()前添加策略配置,解决警告同时规范资源路径:

    qt_policy(SET QTP0001 NEW)
    qt_standard_project_setup()
    
  3. 移除路径中的空格
    将项目目录重命名为不含空格的名称(比如O:\CGPACalculator),避免Windows路径空格解析问题

  4. 清理构建目录重新生成
    删除现有build目录后重新执行构建流程:

    rm -Recurse .\build
    cmake -G "MinGW Makefiles" -S . -B ".\build"
    cmake --build .\build
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 02:57:02