I want to link my target lib to batch of libs, which locate at one particular directory. But it seems that find_library
command can match only one lib at a time.
Indeed, you can only match one library at a time. However, you can do multiple single-library matches and then put them together into a _LIBRARIES variable. Here is an example snippet:
find_library(MYMODULE_FIRST_LIBRARY NAMES MyLib1
PATHS
/usr/lib
/usr/local/lib
)
find_library(MYMODULE_SECOND_LIBRARY NAMES MyLib2
PATHS
/usr/lib
/usr/local/lib
)
if(MYMODULE_FIRST_LIBRARY AND MYMODULE_SECOND_LIBRARY)
set(MYMODULE_LIBRARIES ${MYMODULE_FIRST_LIBRARY} ${MYMODULE_SECOND_LIBRARY})
set(MYMODULE_FOUND TRUE)
message(STATUS "Found MYMODULE: ${MYMODULE_LIBRARIES}")
mark_as_advanced(MYMODULE_LIBRARIES MYMODULE_FIRST_LIBRARY MYMODULE_SECOND_LIBRARY)
The client code then adds ${MYMODULE_LIBRARIES} to the link line. Note that all of this is using the older style using Find. The more modern approach uses CONFIG and TARGETS rather than Find*.cmake files.
A good place to look for examples of this are the built-in Find*.cmake files installed along with CMake.
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