0

如何指向find_package()我的“自定义”目录$SYSROOT/usr/lib/arm-linux-gnueabihf/以便它可以找到libz.so

我在这个位置有一个图书馆:

/home/user/bla/sysroot/usr/lib/arm-linux-gnueabihf/libz.so

带有可以在此处找到的标题:

/home/user/bla/sysroot/usr/include/zlib.h

使用 CMake 我尝试:

cmake_minimum_required(VERSION 3.18)
find_package(ZLIB REQUIRED)

结果是:

CMake Error at /home/user/cmake/linux/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
  Could NOT find ZLIB (missing: ZLIB_LIBRARY) (found version "1.2.11")

所以 CMake 找不到ZLIB_LIBRARY( libz.so) 但它可以1.2.11从头文件中解析版本 ( )。

我在 CMake 中设置了我的 SYSROOT,如下所示:

SET(CMAKE_FIND_ROOT_PATH "${toolchain_dir}/sysroot/")

但似乎 CMake 不知道 usr/lib/ arm-linux-gnueabihf / libz.so。

我试过的

我试图给 HINTS 或 PATH 来find_package喜欢这个:

find_package(ZLIB REQUIRED HINTS "${sysroot_dir}/usr/lib/arm-linux-gnueabihf/" NO_DEFAULT_PATH)

或像这样:

find_package(ZLIB REQUIRED PATH "${sysroot_dir}/usr/lib/arm-linux-gnueabihf/" NO_DEFAULT_PATH)

但它会导致此错误消息:

CMake Error at CMakeLists.txt:12 (find_package):
  Could not find a package configuration file provided by "ZLIB" with any of
  the following names:

    ZLIBConfig.cmake
    zlib-config.cmake

  Add the installation prefix of "ZLIB" to CMAKE_PREFIX_PATH or set
  "ZLIB_DIR" to a directory containing one of the above files.  If "ZLIB"
  provides a separate development package or SDK, be sure it has been
  installed.

额外信息

这是我使用的工具链文件的内容(针对树莓派 4)-DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain.cmake

# Define our host system
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)

# toolchain
set(toolchain_dir "/home/user/bla")
set(sysroot_dir "${toolchain_dir}/sysroot")

SET(CMAKE_C_COMPILER "${toolchain_dir}/tools/gcc-linaro-7.5.0-2019.12-i686_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc")
SET(CMAKE_CXX_COMPILER "${toolchain_dir}/tools/gcc-linaro-7.5.0-2019.12-i686_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++")

# Define the sysroot path for the RaspberryPi distribution
SET(CMAKE_FIND_ROOT_PATH "${toolchain_dir}/sysroot/")
message(STATUS "${CMAKE_FIND_ROOT_PATH}")

# Use our definitions for compiler tools
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# Search for libraries and headers in the target directories only
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
4

3 回答 3

2

FWIW,我使用这样的工具链文件进行交叉编译:

# cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/arm32.cmake ..

# Define our host system
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR armv61)

# Define the cross compiler locations
set(CMAKE_C_COMPILER arm-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER arm-linux-gnueabi-g++)

# Define the sysroot path for the RaspberryPi distribution in our tools folder
set(CMAKE_FIND_ROOT_PATH $ENV{AARCH64_LINUX_TOOLS_DIR}/aarch64-linux-gnu/sysroot)

# Use our definitions for compiler tools
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

我不太确定,但可能是最后两行覆盖了一些东西:

SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

我会尝试删除那些。

另外,我会尝试定期尝试删除整个构建文件夹并重新开始,因为 CMake 缓存了很多东西。可能是某些东西被缓存了(指向某个库的路径不在您的特定 sysroot 中),这弄乱了配置过程。

于 2021-08-09T07:46:16.690 回答
1

一种检查变量是否符合预期的便捷方法。

message(STATUS "sysroot_dir: ${sysroot_dir}")

您应该更改sysroot's 的值,set(CMAKE_SYSROOT /your/path)而不是set(sysroot /your/path)直接在 cmake 中设置变量。

参考这里

于 2021-08-09T08:35:52.730 回答
0

问题是我忘记添加CMAKE_SYSROOT到我的工具链文件中。没有这个,CMake 无法在$SYSROOT/usr/lib/arm-linux-gnueabihf.

于 2021-08-09T08:12:51.453 回答