Why does find_library use PATH instead of LD_LIBRARY_PATH?

I’m building Qt6, which uses the FindZLIB.cmake module to find libz.so. It ends up finding it in the wrong place. When I tried debugging I noticed that the environment-based paths the cmake uses are based on the PATH variable (which it then strips of /bin), not the LD_LIBRARY_PATH one. Should it not be using the latter?

This is cmake 3.25 on QNX.


That’s a bit surprising. CMake 3.3 added the behavior where find_library() considered locations based on PATH, but it was reverted for non-Windows platforms in CMake 3.6. The latest release (CMake 3.28) reverted it for Windows as well. I don’t know what the implications are for QNX, but if you still see the behavior with CMake 3.28, could you show the debug output from your test which demonstrates PATH still being used in the search?

Built and deployed 3.28.1 from git. Here’s the relevant output:

CMake Debug Log at CMakeLists.txt:4 (find_library):
  find_library called with the following settings:

    NAMES: "z"
    Documentation: Path to a library.
      Only Search Frameworks: 0
      Search Frameworks Last: 0
      Search Frameworks First: 0
      Only Search AppBundle: 0
      Search AppBundle Last: 0
      Search AppBundle First: 0

  find_library considered the following locations:


  The item was found at


-- Configuring done (0.3s)
-- Generating done (0.0s)
-- Build files have been written to: /data/home/elahav/src/cmake_test/build
elahav@meddle:~/src/cmake_test/build$ echo $PATH
elahav@meddle:~/src/cmake_test/build$ echo $LD_LIBRARY_PATH

It still seems to consider PATH first. I removed /proc/boot/libz.so so it doesn’t find it (/proc/boot is the image file system, the QNX equivalent of an initial ramdisk), but would have detected that copy of the library first had it still been there.

I miscalled the situation regarding PATH usage. According to the latest find_library() docs, PATH is still searched. Only some of the additional paths based on PATH were removed. Here’s the relevant part of the docs which explain why you’re seeing PATH being used:

Search the standard system environment variables. This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is passed or by setting the CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH to FALSE.

  • The directories in LIB and PATH.

On Windows hosts, CMake 3.3 through 3.27 searched additional paths: /lib/ if CMAKE_LIBRARY_ARCHITECTURE is set, and /lib for each /[s]bin in PATH, and /lib for other entries in PATH. This behavior was removed by CMake 3.28.

Thanks, Craig.
Still seems odd to me that PATH is searched at all, let alone before LD_LIBRARY_PATH.