如何测试CMake是否通过find_library找到了一个库 [英] How to test if CMake found a library with find_library

查看:892
本文介绍了如何测试CMake是否通过find_library找到了一个库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到具有 find_library 函数的库

find_library(MY_LIB lib PATHS ${MY_PAHT})

如果找到库, $ {MY_LIB} 将指向正确的位置。
如果未找到库,则 $ {MY_LIB} 将为 MY_LIB-NOTFOUND

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.

但是如何测试呢?

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

to false。

推荐答案

您可以简单地测试变量,例如:

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()

输出示例:

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


-- Configuring incomplete, errors occurred!

请注意,我们使用

if(NOT LUA_LIB)

而不是

if(NOT ${LUA_LIB})


$ b b

,因为不同的语义

使用 $ {} ,变量 LUA_LIB code> if()被求值。作为评估的
部分,内容将被解释为变量名称,
,除非它匹配常量的定义。这不是我们想要的。

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.

这篇关于如何测试CMake是否通过find_library找到了一个库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆