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

Yocto构建中用CMake install安装pybind11模块遇QA问题求助

解决Yocto打包pybind11模块的QA错误问题

问题背景

通过pybind11将自研C++库封装为Python模块,手动复制pybind11_example.so到目标设备可正常使用,但使用CMake的install(TARGETS...)指令实现自动安装时,Yocto构建过程中出现QA错误。

现有CMakeLists.txt代码

cmake_minimum_required(VERSION 3.14.4)
project(pybind11_example)

set(CMAKE_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../lib/math/include")

find_package(PythonLibs REQUIRED)
set(PYTHON_MODULE_EXTENSION ".so" CACHE INTERNAL "Cross python lib extension")

find_package(pybind11 REQUIRED)
pybind11_add_module(${PROJECT_NAME} pybind11_wrapper.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_INCLUDE_PATH})
target_link_libraries(${PROJECT_NAME} PRIVATE simplemath)

include(GNUInstallDirs)

# without lines below, yocto build works fine
install(TARGETS ${PROJECT_NAME}
  COMPONENT python
  RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
  LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
  ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")

构建错误信息

执行devtool build时抛出以下QA错误:

ERROR: pybind11-example-1.0+git999-r0 do_package: QA Issue: pybind11-example: Files/directories were installed but not shipped in any package:
  /usr/lib/pybind11_example.so
Please set FILES such that these items are packaged. Alternatively if they are unneeded, avoid installing them or delete them within do_install.
pybind11-example: 1 installed and not shipped files. [installed-vs-shipped]
ERROR: pybind11-example-1.0+git999-r0 do_package: Fatal QA errors found, failing task.

解决方案

错误原因是Yocto无法识别安装的pybind11_example.so属于哪个软件包,提供两种可行解决思路:

方法一:将模块安装到Python标准路径(推荐)

Python模块默认应安装到site-packages目录,而非通用的/usr/lib,这样既符合Python规范,也能让Yocto的Python打包逻辑自动识别文件。修改CMakeLists.txt中的安装配置:

  1. 先获取Python的site-packages路径:

    # 获取Python site-packages路径
    execute_process(
        COMMAND "${PYTHON_EXECUTABLE}" -c "import site; print(site.getsitepackages()[0])"
        OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    
  2. 调整install(TARGETS...)指令,将模块安装到上述路径:

    install(TARGETS ${PROJECT_NAME}
        COMPONENT python
        LIBRARY DESTINATION "${PYTHON_SITE_PACKAGES}"
        # 移除不需要的RUNTIME/ARCHIVE安装项,因为Python模块是共享库
    )
    

方法二:在Yocto Recipe中配置文件归属

如果需要保留现有安装路径,需在对应的Yocto Recipe(.bb或.bbappend文件)中明确指定文件所属的软件包:

  1. 若要将文件归到主软件包:

    FILES_${PN} += "/usr/lib/pybind11_example.so"
    
  2. 若要单独创建Python组件包:

    # 添加Python组件包
    PACKAGES += "${PN}-python"
    # 指定组件包包含的文件
    FILES_${PN}-python = "/usr/lib/pybind11_example.so"
    # 设置组件包的依赖
    RDEPENDS_${PN}-python += "python3-core"
    

额外检查

确保Yocto Recipe中已添加inherit cmake,保证CMake构建流程被正确处理。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 19:35:18