'find_library' returns the same value in the loop in CMake

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?


Result of find_library is 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}) # 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.


Solved by:


See here for a similar problem.


  • How do we add facebookSDK into a xcode project using cmake?
  • Git repository structure and modularity
  • Azure Stack Table Storage: Value for HTTP headers not in correct format
  • How to add plugin dependencies to pom.xml
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • List images(01.png) and descriptions(01.txt) from directory
  • How to make jdk.nashorn.api.scripting.JSObject visible in plugin [duplicate]
  • ADO and msqli connections very slow
  • PHP buffered output depending on server setting?
  • D3 nodes and links from JSON with nested arrays of children
  • How to add date and time under each post in guestbook in google app engine
  • Change an a tag attribute in JavaScript based on screen width
  • How to show dropdown in excel using jrxml (jasper api)?
  • Convert array of 8 bytes to signed long in C++
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • jquery mobile loadPage not working
  • Apache 2.4 - remove | delete | uninstall
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal