'find_library' returns the same value in t

2019-07-15 03:37发布

问题:

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?

回答1:

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.



回答2:

Solved by:

UNSET(LIB_NAME CACHE)

See here for a similar problem.



标签: cmake