sighol
sighol

Reputation: 2818

How to test if CMake found a library with find_library

I find the library with the find_library function

find_library(MY_LIB lib PATHS ${MY_PATH})

If the library is found, ${MY_LIB} will point to the correct location. If the library is not found ${MY_LIB} will be MY_LIB-NOTFOUND.

But how do I test this?

if(${MY_LIB} EQUAL 'MY_LIB-NOTFOUND') 
    ...
endif()

always evaluates to false.

Upvotes: 44

Views: 43788

Answers (2)

maxschlepzig
maxschlepzig

Reputation: 39047

You can simply test the variable as such, e.g.:

find_library(LUA_LIB lua)
if(NOT LUA_LIB)
  message(FATAL_ERROR "lua library not found")
endif()

Example output:

CMake Error at CMakeLists.txt:99 (message):
  lua library not found


-- Configuring incomplete, errors occurred!

Note that we use

if(NOT LUA_LIB)

and not

if(NOT ${LUA_LIB})

because of the different semantics.

With ${}, the variable LUA_LIB is substitued before if() is evaluated. As part of the evaluation the content would then be interpreted as variable name, unless it matches the definition of a constant. And this isn't what we want.

Upvotes: 65

NaCl
NaCl

Reputation: 2713

Simply do

if(MY_LIB)
    #found
    ...
else()
    #not found
    ...
endif()

Upvotes: 11

Related Questions