CMake's find_library() ignoring any specified path

Viewed 37

I'm trying to find a library that's built alongside a subsection of a project, the library is built with a "externalproject_add" call and builds successfully. It puts a library (.so.2) in a subdirectory within itself

I've specified a directory with PATHS (I've also tried HINTS, and adding the trailing /lib as part of the path), and am using debug mode to get CMake to print the information as it tries to find this library.

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

I'm very clear here about the path I have my library in. I've also tried suspending the leading lib, trailing *.so.2, etc to no avail, it looks like CMake isn't even trying:

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.

If I supply /usr/bin as a path to PATHS as an experiment, CMake appends that to some output directory somewhere deep within the project, even though it's an absolute path.

I'm finding CMake to be extremely counterintuitive. I've given it an exact location where there is a library, how can I get it to actually look in that directory?

Edit

IT seems find_library is modifying the paths for some reason:

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'
1 Answers

I'm not sure what else is going on in your CMakeLists.txt, but I can't replicate your failure with the code and paths you provided.

First, I created a CMakeLists.txt file with the following contents:

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}")

Then, I replicated the directory layout described in your post and created an empty library file with the appropriate name:

$ 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

Finally, I configured the build using Ninja and the --debug-find-var option of CMake 3.23 to see how the search proceeded.

$ 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

And this apparently works.

Related