Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use find_library command to find multiple libs in Cmake?

Tags:

c++

cmake

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.

like image 746
WilliamAllwaysWin Avatar asked Oct 18 '25 09:10

WilliamAllwaysWin


1 Answers

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.

like image 102
Russell Taylor Avatar answered Oct 19 '25 23:10

Russell Taylor