Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test if CMake found a library with find_library

Tags:

cmake

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.

like image 729
sighol Avatar asked Apr 15 '15 17:04

sighol


People also ask

What does Find_library do in CMake?

This command is used to find a library. A cache entry, or a normal variable if NO_CACHE is specified, named by <VAR> is created to store the result of this command. If the library is found the result is stored in the variable and the search will not be repeated unless the variable is cleared.


1 Answers

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.

like image 121
maxschlepzig Avatar answered Sep 28 '22 20:09

maxschlepzig