Debian 12交叉编译时CMake无法找到sysroot中的xkbcommon-x11
Debian 12 WSL交叉编译无法找到xkbcommon-x11库的问题排查与解决
在Debian 12(WSL)主机上进行AArch64交叉编译时,CMake无法定位xkbcommon-x11库,但相同的sysroot和CMake版本(3.28.3)在Ubuntu 24.04(WSL)上可正常工作。以下是测试用的相关文件、输出及解决方案:
测试文件
CMakeLists.txt
cmake_minimum_required(VERSION 3.18) project(Test) find_package(PkgConfig QUIET) # pkg_check_modules(XKB_COMMON_X11 IMPORTED_TARGET "xkbcommon-x11>=0.4.1") pkg_check_modules(XKB_COMMON_X11 IMPORTED_TARGET "xkbcommon-x11") if (NOT TARGET PkgConfig::XKB_COMMON_X11) set(XKB_COMMON_X11_FOUND 0) endif() message("XKB_COMMON_X11_FOUND: ${XKB_COMMON_X11_FOUND}") message("XKB_COMMON_X11_VERSION: ${XKB_COMMON_X11_VERSION}")
toolchain.cmake
cmake_minimum_required(VERSION 3.18) include_guard(GLOBAL) set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR aarch64) IF(NOT DEFINED ENV{sroot}) message(FATAL_ERROR "Please set the environment variable sroot to the path of the sysroot") ENDIF() IF (NOT EXISTS $ENV{sroot}/usr/include) message(FATAL_ERROR "It seems that the sysroot image is not mounted. Please mount it first.") ENDIF() set(TARGET_SYSROOT $ENV{sroot}) set(CMAKE_SYSROOT ${TARGET_SYSROOT}) set(ENV{PKG_CONFIG_PATH} $PKG_CONFIG_PATH:/usr/lib/aarch64-linux-gnu/pkgconfig) set(ENV{PKG_CONFIG_LIBDIR} /usr/lib/pkgconfig:/usr/share/pkgconfig/:${TARGET_SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig:${TARGET_SYSROOT}/usr/lib/pkgconfig) set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT}) # if you use other version of gcc and g++ than gcc/g++ 9, you must change the following variables set(CMAKE_C_COMPILER /usr/bin/aarch64-linux-gnu-gcc-12) set(CMAKE_CXX_COMPILER /usr/bin/aarch64-linux-gnu-g++-12) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem=/usr/include -isystem=/usr/include/aarch64-linux-gnu") set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}") set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) set(CMAKE_BUILD_RPATH ${TARGET_SYSROOT})
运行命令
cmake . --toolchain toolchain.cmake
输出对比
Ubuntu 24.04 输出
-- The C compiler identification is GNU 12.3.0 -- The CXX compiler identification is GNU 12.3.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/aarch64-linux-gnu-gcc-12 - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++-12 - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done -- Checking for module 'xkbcommon-x11' -- Found xkbcommon-x11, version 1.5.0 XKB_COMMON_X11_FOUND: 1 XKB_COMMON_X11_VERSION: 1.5.0 -- Configuring done (7.7s) -- Generating done (0.0s)
Debian 12 输出
-- The C compiler identification is GNU 12.2.0 -- The CXX compiler identification is GNU 12.2.0 -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working C compiler: /usr/bin/aarch64-linux-gnu-gcc-12 - skipped -- Detecting C compile features -- Detecting C compile features - done -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++-12 - skipped -- Detecting CXX compile features -- Detecting CXX compile features - done XKB_COMMON_X11_FOUND: 0 XKB_COMMON_X11_VERSION: -- Configuring done -- Generating done
更新信息
Ubuntu上验证库来源,确认是sysroot中的文件:
PkgConfig::XKB_COMMON_X11 INTERFACE_LINK_LIBRARIES = /home/a/buildimg/root/usr/lib/aarch64-linux-gnu/libxkbcommon-x11.so;/home/a/buildimg/root/usr/lib/aarch64-linux-gnu/libxkbcommon.so
原因分析
- Debian缺少交叉编译依赖包:Ubuntu默认预装了xkbcommon-x11的交叉编译pkg-config文件,而Debian需要手动安装对应架构的开发包。
- pkg-config环境变量配置问题:Debian对
PKG_CONFIG_PATH/PKG_CONFIG_LIBDIR的解析逻辑与Ubuntu存在差异,导致无法正确定位sysroot中的.pc文件。
解决步骤
步骤1:安装Debian交叉编译开发包
在Debian 12 WSL中执行以下命令,安装AArch64架构的xkbcommon-x11开发包:
sudo apt install libxkbcommon-x11-dev:aarch64
步骤2:调整toolchain.cmake的pkg-config环境变量
修改环境变量配置,确保pkg-config优先从sysroot查找,避免主机路径干扰:
# 清空原有路径,仅保留sysroot相关的pkg-config目录 set(ENV{PKG_CONFIG_PATH} "${TARGET_SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig:${TARGET_SYSROOT}/usr/lib/pkgconfig") # 明确指定pkg-config的库目录 set(ENV{PKG_CONFIG_LIBDIR} "${TARGET_SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig:${TARGET_SYSROOT}/usr/lib/pkgconfig") # 确保sysroot目录正确设置 set(ENV{PKG_CONFIG_SYSROOT_DIR} ${CMAKE_SYSROOT})
步骤3:手动验证pkg-config可用性(可选)
在Debian WSL中直接运行pkg-config命令,测试是否能定位到库:
PKG_CONFIG_SYSROOT_DIR=$sroot PKG_CONFIG_LIBDIR=${sroot}/usr/lib/aarch64-linux-gnu/pkgconfig pkg-config --modversion xkbcommon-x11
如果输出版本号,说明配置生效,重新运行CMake即可。
步骤4:直接指定pc文件路径( fallback方案)
若上述方法无效,可在CMakeLists.txt中直接指定.pc文件路径:
pkg_check_modules(XKB_COMMON_X11 IMPORTED_TARGET "${TARGET_SYSROOT}/usr/lib/aarch64-linux-gnu/pkgconfig/xkbcommon-x11.pc")
内容的提问来源于stack exchange,提问作者maestro
相关产品推荐
相关产品推荐

