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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With