I used a list
to store names of libraries, and I would like to use foreach
and find_library
to find full path of each library. But find_library
just returned the path of the first library. I checked this post, but problem still exist. My CMake version is 3.4.3.
SET(VTKLIBS_DIR)
FOREACH(LIB ${VTKLIBS})
SET(FOUND_LIB)
FIND_LIBRARY(FOUND_LIB ${LIB})
LIST(APPEND VTKLIBS_DIR ${FOUND_LIB})
MESSAGE("Lib: ${LIB}")
MESSAGE("Found Lib: ${FOUND_LIB}")
UNSET(FOUND_LIB)
ENDFOREACH(LIB)
Command
find_library
sets cached variable, but simple form of commandunset
remove only simple variable's definition.As noted by the link you provide, you need to store special value
FOUND_LIB-NOTFOUND
to the variableFOUND_LIB
for forcefind_library
to search another library while variable already contains path to the previous library:Actually, this is some kind of trick, as cached variable
FOUND_LIB
isn't changed by simpleset
command. But whenfind_library
implementation attempts to read cached value of the variable, it actually read value of simple variable with the same name.Because
find_library
treats only*-NOTFOUND
cached values as "library not found", your trick with assigning empty value to the variable doesn't work.The better approach, as noted by @arrowd, would be using different names for variable, used in different
find_library()
call:Such a way results for every
find_library
call will be stored separately, and the same library will not be searched again at the next time cmake being invoked. Also, such approach allows user to modify (in cache) paths to concrete libraries without affecting other ones.