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

将QML项目编译打包进Yocto镜像时遭遇配置问题

解决Yocto打包QML项目时的installed-vs-shipped错误

问题根源

  1. FILES变量位置错误:原配方中将FILES:${PN}写在do_install函数内部,Yocto无法识别这个全局配置,导致已安装的文件未被标记为需要打包的内容。
  2. 路径匹配不完整:原FILES仅指定了子目录名称,未覆盖目录下的所有文件及子目录。
  3. 重复安装操作:配方的do_install和CMake的install指令重复安装了字体、图片资源,引发冗余。
  4. QML文件处理不当:原CMake仅编译了C++代码,未正确处理QML文件的部署或编译。

修复步骤

1. 修正Yocto配方文件(coffeemachine_0.1.bb)

SUMMARY = "QML Coffee Machine"
LICENSE = "CLOSED"

inherit qt6-cmake

SRC_URI = "file://CMakeLists.txt                \
           file://qmlmodules                    \
           file://main.qml                      \
           file://CoffeeMachine.qmlproject      \
           file://qtquickcontrols2.conf         \
           file://content/                      \
           file://imports/                      \
           file://src/                          \
          "

DEPENDS += "qtbase                      \
            qtdeclarative               \
            qtdeclarative-native        \
            qtquick3d                   \
            qtquicktimeline             \
           "

S = "${WORKDIR}"

do_install() {
  # 创建程序目录
  install -d ${D}${bindir}/coffeemachine
  # 安装CMake编译出的可执行文件(注意文件名对应CMake中的target)
  install -m 0755 CoffeeMachineApp ${D}${bindir}/coffeemachine/coffeemachine

  # 安装QML文件和配置
  install -m 0644 ${S}/main.qml ${D}${bindir}/coffeemachine/
  install -d ${D}${bindir}/coffeemachine/content/
  install -m 0644 ${S}/content/*.qml ${D}${bindir}/coffeemachine/content/
  install -m 0644 ${S}/content/qtquickcontrols2.conf ${D}${bindir}/coffeemachine/content/
  install -m 0644 ${S}/qtquickcontrols2.conf ${D}${bindir}/coffeemachine/
}

# 全局配置需要打包的文件,覆盖整个程序目录和资源目录
FILES:${PN} += " \
    ${bindir}/coffeemachine/ \
    ${datadir}/content/ \
"

2. 修正CMakeLists.txt

使用Qt6官方CMake API处理QML和资源,避免硬编码路径:

cmake_minimum_required(VERSION 3.18)

project(CoffeeMachine LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON) # 启用资源文件编译

find_package(Qt6 REQUIRED COMPONENTS Gui Qml Quick)

# 添加可执行文件
qt_add_executable(CoffeeMachineApp
    src/main.cpp
)

# 处理QML模块,可选择将QML编译进二进制(静态加载)
qt_add_qml_module(CoffeeMachineApp
    URI CoffeeMachine
    VERSION 1.0
    QML_FILES
        main.qml
        content/*.qml
        imports/CoffeeMachine/*.qml
    RESOURCES
        content/fonts/*
        content/images/**
)

target_link_libraries(CoffeeMachineApp PRIVATE
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Quick
    Qt${QT_VERSION_MAJOR}::Qml
)

# 安装可执行文件到Yocto指定的bin目录子文件夹
install(TARGETS CoffeeMachineApp
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}/coffeemachine
)

# 安装资源到标准datadir路径,使用CMake变量避免硬编码
install(DIRECTORY content/fonts/
    DESTINATION ${CMAKE_INSTALL_DATADIR}/content/fonts
    FILES_MATCHING PATTERN "*.ttf" PATTERN "fonts.txt"
)

install(DIRECTORY content/images/
    DESTINATION ${CMAKE_INSTALL_DATADIR}/content/images
    FILES_MATCHING PATTERN "*.png"
)

额外注意事项

  • 可执行文件名需与CMake中target名称一致(原CMake中是CoffeeMachineApp,配方中需对应)。
  • 若希望QML完全编译进二进制(不暴露源码),可在qt_add_qml_module中添加STATIC选项。
  • 修复后可执行bitbake coffeemachine -c cleanall && bitbake coffeemachine重新构建。
  • 可通过bitbake coffeemachine -c install -f查看安装后的文件结构,确认资源路径是否正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 13:08:11