ant2009
ant2009

Reputation: 22696

force cmake FIND_LIBRARY to look in custom directory

cmake version 2.8.4

I have the following apache portable runtime libraries that I have compiled myself and want my application to link against.

My project directory where my apr libraries are:

gw_proj/tools/apr/libs

In my CMakeLists.txt I have the following:

FIND_LIBRARY(APRUTIL NAMES "aprutil-1"
  PATHS ${PROJECT_SOURCE_DIR}/tools/apr/libs)

My problem is on a machine that already has the apache portable runtime already installed it will look for it in this folder:

/usr/lib

So will always ignore my custom path.

How can I force the FIND_LIBRARY to always look in my custom directory:

gw_proj/tools/apr/libs

Many thanks for any suggestions

Upvotes: 27

Views: 22614

Answers (1)

Fraser
Fraser

Reputation: 78488

You can specify the search order using one or more of NO_DEFAULT_PATH, NO_CMAKE_ENVIRONMENT_PATH , NO_CMAKE_PATH, NO_SYSTEM_ENVIRONMENT_PATH, NO_CMAKE_SYSTEM_PATH, CMAKE_FIND_ROOT_PATH_BOTH, ONLY_CMAKE_FIND_ROOT_PATH, orNO_CMAKE_FIND_ROOT_PATH.

From the docs for find_library:

The default search order is designed to be most-specific to least-specific for common use cases. Projects may override the order by simply calling the command multiple times and using the NO_* options:

find_library(<VAR> NAMES name PATHS paths... NO_DEFAULT_PATH)
find_library(<VAR> NAMES name)

Once one of the calls succeeds the result variable will be set and stored in the cache so that no call will search again.

So in your case, you can do

FIND_LIBRARY(APRUTIL NAMES "aprutil-1"
  PATHS ${PROJECT_SOURCE_DIR}/tools/apr/libs NO_DEFAULT_PATH)

Upvotes: 26

Related Questions