基于Yocto构建依赖libmariadb的C++应用遇CMake配置问题
问题背景
正在为依赖libmariadb的C++应用编写Yocto配方,开发阶段使用vcpkg,迁移至Yocto环境后遇到CMake配置及依赖编译问题。
当前Yocto配方(application_1.0.0.bb)
# Metadata SUMMARY = "Customer API Backend" DESCRIPTION = "Customer API in C++ using gRPC" # License is closed, no checksum to avoid warnings LICENSE = "CLOSED" LIC_FILE_CHKSUM = "" DEPENDS = "protobuf protobuf-c protobuf-native grpc grpc-native openssl mariadb poco" SRCREV = "${AUTOREV}" SRC_URI = "git://git@gitlab.com/software/projects/embedded/application.git;protocol=ssh;branch=master;" S = "${WORKDIR}/git" inherit pkgconfig cmake
与Yocto不兼容的CMake代码片段
find_package(unofficial-libmariadb CONFIG REQUIRED) find_package(Poco REQUIRED COMPONENTS Data DataMySQL) target_link_libraries(database_api PRIVATE unofficial::libmariadb Poco::DataMySQL )
第一个错误:CMake找不到unofficial-libmariadb
CMake Error at src/libraries/external_interfaces/database_api/CMakeLists.txt:20 (find_package):
Could not find a package configuration file provided by
"unofficial-libmariadb" with any of the following names:unofficial-libmariadbConfig.cmake unofficial-libmariadb-config.cmakeAdd the installation prefix of "unofficial-libmariadb" to CMAKE_PREFIX_PATH
or set "unofficial-libmariadb_DIR" to a directory containing one of the
above files. If "unofficial-libmariadb" provides a separate development
package or SDK, be sure it has been installed.
添加mariadb-native后出现的新错误
CMake Error at /home/oe-core/build/tmp/work/x86_64-linux/mariadb-native/10.7.5-r0/recipe-sysroot-native/usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find GnuTLS (missing: GNUTLS_LIBRARY GNUTLS_INCLUDE_DIR)
(Required is at least version "3.3.24")
Call Stack (most recent call first):
(...)
-- Configuring incomplete, errors occurred!
解决方案
1. 替换CMake中libmariadb的查找逻辑
Yocto中的mariadb包不提供unofficial-libmariadb的CMake配置文件(该名称为vcpkg特有),需改用标准查找方式:
# 方案1:使用MariaDB官方CMake模块 find_package(MariaDB REQUIRED) find_package(Poco REQUIRED COMPONENTS Data DataMySQL) target_link_libraries(database_api PRIVATE MariaDB::MariaDB Poco::DataMySQL )
如果官方模块不生效,可改用pkg-config:
# 方案2:通过pkg-config查找 find_package(PkgConfig REQUIRED) pkg_check_modules(LIBMARIADB REQUIRED IMPORTED_TARGET libmariadb) find_package(Poco REQUIRED COMPONENTS Data DataMySQL) target_link_libraries(database_api PRIVATE PkgConfig::LIBMARIADB Poco::DataMySQL )
2. 修复mariadb-native的依赖缺失问题
若必须保留mariadb-native,需添加其依赖的gnutls-native包到配方:
DEPENDS = "protobuf protobuf-c protobuf-native grpc grpc-native openssl mariadb poco gnutls-native"
如果无需native版本的mariadb(多数场景下目标机的mariadb包已提供足够开发文件),直接移除mariadb-native即可避免该问题。
3. 可选:强制指定CMake查找路径
若CMake仍无法定位MariaDB,可在配方中通过EXTRA_OECMAKE传递路径参数:
EXTRA_OECMAKE += "-DMariaDB_DIR=${STAGING_DIR_HOST}/usr/lib/cmake/MariaDB"
验证步骤
- 修改CMakeLists.txt中的libmariadb查找逻辑
- 更新Yocto配方的DEPENDS配置
- 执行
bitbake application重新编译
内容的提问来源于stack exchange,提问作者mmnano50

