BhanuPrakash
BhanuPrakash

Reputation: 11

Is there anyway to find library path dynamically in CMake?

I have libcurl.so in three different paths. lets say /usr/lib , /opt/a/.../lib/, /opt/b/.../lib/

I want to link proper library while building. How to write CMakeLists.txt to do that?

As of now I have hard coded to find it in /usr/lib/

project (mylib)

find_library(LIB_CURL_LIBRARY NAMES curl HINTS "/usr/lib/")

target_link_libraries (mylib curl)

Upvotes: 1

Views: 336

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 65870

Command find_library actually searches many places, which can also be adjusted by a user (the person, who uses the CMake project with find_library call, but do not modifies CMakeLists.txt).

Also, via HINTS and PATHS you (as the project's developer) may add more hints to search, and these hints could also be made modifiable by a user.

You may find complete description about search paths in documentation.

When decide how to make find_library to search in the specific path, you need to "classify" origin that path. Some common cases:

  1. Is the path a standard one for specific OS or distro? If so, CMake usually searches this path by default.

  2. Does the path come from the custom installation of the package? If so, the user could assign installation prefix to some variable (e.g. XXX_ROOT, where XXX is a package name or abbreviation), which is used as PATHS or HINTS in your find_library call.

  3. Does the path come from the custom installation prefix, common for many packages? If so, the user could assign that common prefix to CMAKE_PREFIX_PATH variable, and find_library automatically will take this prefix into account.

Note, that find_library is usually used in a FindXXX.cmake module (which is activated via find_package(XXX)). Such module could include additional logic for find additional possible library locations according to the system introspection.

Upvotes: 1

Related Questions