CMake 的 find_library() 忽略任何指定的路径

问题描述 投票:0回答:2

我正在尝试找到一个与项目的一部分一起构建的库,该库是使用“externalproject_add”调用构建的并成功构建。它将库 (.so.2) 放入其自身的子目录中

我已经使用 PATHS 指定了一个目录(我也尝试过 HINTS,并添加尾随 /lib 作为路径的一部分),并且正在使用调试模式让 CMake 在尝试查找此库时打印信息。

find_library(METADX1_LIB NAMES libmdx1host.so.2 PATHS ${CMAKE_CURRENT_SOURCE_DIR}/meta-dx1/sdk/builds/debug/ PATH_SUFFIXES lib NO_DEFAULT_PATH)

我在这里非常清楚我的库所在的路径。我也尝试过暂停前导

lib
、尾随
*.so.2
等,但无济于事,看起来 CMake 甚至没有尝试:

CMake Debug Log at Sub-project/CMakeLists.txt:187 (find_library):
find_library called with the following settings:

VAR: METADX1_LIB
NAMES: "libmdx1host.so.2"
Documentation: Path to a library.
Framework
  Only Search Frameworks: 0
  Search Frameworks Last: 0
  Search Frameworks First: 0
AppBundle
  Only Search AppBundle: 0
  Search AppBundle Last: 0
  Search AppBundle First: 0
NO_DEFAULT_PATH Enabled

find_library considered the following locations:

The item was not found.

如果我提供

/usr/bin
作为 PATHS 的路径作为实验,CMake 会将其附加到项目深处某处的某个输出目录,即使它是绝对路径。

我发现 CMake 极其违反直觉。我已经给了它一个有库的确切位置,我怎样才能让它实际在该目录中查找?

find_library 似乎由于某种原因正在修改路径:

CMake Debug Log at TMX-4400/CMakeLists.txt:190 (find_library):
find_library(METADX1_LIB) removed original suffix 

/home/brydon/embedded/product/../../tools/optelinux/bdrt/output/rootfs.tmx4400/staging/home/brydon/embedded/product/TMX-4400/meta-dx1/sdk/builds/debug/

from PATH_SUFFIXES while adding architecture paths for suffix '64'
cmake
2个回答
0
投票

我不确定您的 CMakeLists.txt 中还发生了什么,但我无法使用您提供的代码和路径复制您的失败。

首先,我创建了一个

CMakeLists.txt
文件,其中包含以下内容:

cmake_minimum_required(VERSION 3.23)
project(test)

find_library(
  METADX1_LIB
  NAMES libmdx1host.so.2
  PATHS ${CMAKE_CURRENT_SOURCE_DIR}/meta-dx1/sdk/builds/debug/
  PATH_SUFFIXES lib
  NO_DEFAULT_PATH
)

message(STATUS "METADX1_LIB = ${METADX1_LIB}")

然后,我复制了您帖子中描述的目录布局,并创建了一个具有适当名称的空库文件:

$ mkdir -p meta-dx1/sdk/builds/debug
$ touch meta-dx1/sdk/builds/debug/libmdx1host.so.2
$ tree
.
├── CMakeLists.txt
└── meta-dx1
    └── sdk
        └── builds
            └── debug
                └── libmdx1host.so.2

最后,我使用 Ninja 和 CMake 3.23 的

--debug-find-var
选项配置了构建,以查看搜索如何进行。

$ cmake -G Ninja -S . -B build --debug-find-var=METADX1_LIB
Running with debug output on for the variable(s) METADX1_LIB.
-- The C compiler identification is GNU 11.3.0
-- The CXX compiler identification is GNU 11.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Debug Log at CMakeLists.txt:4 (find_library):
  find_library called with the following settings:

    VAR: METADX1_LIB
    NAMES: "libmdx1host.so.2"
    Documentation: Path to a library.
    Framework
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
    AppBundle
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0
    NO_DEFAULT_PATH Enabled

  find_library considered the following locations:

    /home/alex/test2/meta-dx1/sdk/builds/debug/lib//(lib)libmdx1host.so.2(\.so|\.a)
    /home/alex/test2/meta-dx1/sdk/builds/debug/lib/(lib)libmdx1host.so.2(\.so|\.a)

  The item was found at

    /home/alex/test2/meta-dx1/sdk/builds/debug/



-- METADX1_LIB = /home/alex/test2/meta-dx1/sdk/builds/debug/libmdx1host.so.2
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/test2/build

这显然有效。


0
投票

库位置由 cmake 缓存,因此如果您添加了路径位置并且 cmake 之前在其他地方找到了该库,请确保清除构建目录。

© www.soinside.com 2019 - 2024. All rights reserved.