How to test if CMake found a library with find_library

前端 未结 2 1503
忘了有多久
忘了有多久 2020-12-30 20:25

I find the library with the find_library function

find_library(MY_LIB lib PATHS ${MY_PATH})

If the library is found, ${MY_

相关标签:
2条回答
  • 2020-12-30 20:56

    Simply do

    if(MY_LIB)
        #found
        ...
    else()
        #not found
        ...
    endif()
    
    0 讨论(0)
  • 2020-12-30 21:12

    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.

    0 讨论(0)
提交回复
热议问题