'find_library'在CMake的循环中返回相同的值 [英] 'find_library' returns the same value in the loop in CMake

查看:61
本文介绍了'find_library'在CMake的循环中返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历带有CMake中库名称的列表.在每次迭代中,我都使用 find_library 搜索该库:

  set(LIB_NAMES"TKBO; TKBRep;")设置(LIBS_DIR/usr/local/OCCT/7.2.0/libd)FOREACH(LIB_NAME $ {LIB_NAMES})FIND_LIBRARY(LIB $ {LIB_NAME}路径$ {LIBS_DIR})MESSAGE(<< $ {LIB_NAME}>>"))MESSAGE(<< $ {LIB}>")target_link_libraries(mySharedLib $ {LIB})ENDFOREACH() 

对于以上内容,我得到了输出:

 << TKBO>><//usr/local/OCCT/7.2.0/libd/libTKBO.dylib>><< TKBRep>>><//usr/local/OCCT/7.2.0/libd/libTKBO.dylib>> 

LIB_NAME更新时, FIND_LIBRARY 似乎使用了过时的值.我还尝试在循环结束时显式 UNSET(LIB_NAME),但这也无济于事.

我可以忽略什么?

解决方案

find_library 的结果是一个 CACHED 变量,一旦找到该库,该变量即为未更新.

搜索不同的库时,最好使用不同的结果变量:

  FOREACH(LIB_NAME $ {LIB_NAMES})set(LIB_VAR"LIB _ $ {LIB_NAME}")#存储搜索结果的变量的名称FIND_LIBRARY($ {LIB_VAR} $ {LIB_NAME}路径$ {LIBS_DIR})target_link_libraries(mySharedLib $ {$ {LIB_VAR}})ENDFOREACH() 

此处 LIB_TKBO 变量用于 TKBO 库,而 LIB_TKBRep 变量-用于 TKBRep 库.

I'm trying to loop over a list with library names in CMake. In each iteration I search the library with find_library:

set(LIB_NAMES "TKBO;TKBRep;")
set(LIBS_DIR /usr/local/OCCT/7.2.0/libd)


FOREACH(LIB_NAME ${LIB_NAMES})
  FIND_LIBRARY(LIB ${LIB_NAME} PATHS ${LIBS_DIR})
  MESSAGE("<<${LIB_NAME}>>")
  MESSAGE("<<${LIB}>>")
  target_link_libraries(mySharedLib ${LIB})
ENDFOREACH()

For the above, I get the output:

<<TKBO>>
<</usr/local/OCCT/7.2.0/libd/libTKBO.dylib>>
<<TKBRep>>
<</usr/local/OCCT/7.2.0/libd/libTKBO.dylib>>

While LIB_NAME updates, FIND_LIBRARY seems to be using an outdated value. I also tried to explicitly UNSET(LIB_NAME) at the end of the loop but that didn't help either.

What could I be overlooking?

解决方案

The result of find_library is a CACHED variable, and once the library is found, the variable is not updated.

When search different libraries, it is better to use different result variables:

FOREACH(LIB_NAME ${LIB_NAMES})
  set(LIB_VAR "LIB_${LIB_NAME}") # Name of the variable which stores result of the search
  FIND_LIBRARY(${LIB_VAR} ${LIB_NAME} PATHS ${LIBS_DIR})
  target_link_libraries(mySharedLib ${${LIB_VAR}})
ENDFOREACH()

Here LIB_TKBO variable is used for TKBO library, and LIB_TKBRep variable - for TKBRep library.

这篇关于'find_library'在CMake的循环中返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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