且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何测试CMake是否通过find_library找到了一个库

更新时间:2023-02-03 12:37:13

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

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.