CMake find_library matching behavior?

2019-01-18 13:30发布

问题:

One specifies find_library( name PATHS path1..pathn)

My question is how does find_library match name to the library file (on Windows and Linux)?

For example, I am unable to have find_library identify the MagicK and MagicK++ dll files in the provided windows binary installation of GraphicsMagicK:

The files is: CORE_RL_magick_.dll

Searching for the queries:

magick CORE_RL_magick

does not work.

回答1:

You might want to take a look at this documentation links:

http://www.cmake.org/cmake/help/v2.8.10/cmake.html#command:find_library

http://www.cmake.org/cmake/help/v2.8.10/cmake.html#variable:CMAKE_FIND_LIBRARY_PREFIXES

http://www.cmake.org/cmake/help/v2.8.10/cmake.html#variable:CMAKE_FIND_LIBRARY_SUFFIXES

find_library may accept one or more library names. Those names get the value of CMAKE_FIND_LIBRARY_PREFIXES prepended and CMAKE_FIND_LIBRARY_SUFFIXES appended. This two variables should be set for each OS depending on how the librares are prefixed or suffixed there.

In your case I'd write for Windows

SET(CMAKE_FIND_LIBRARY_PREFIXES "")
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".dll")

and for Linux

SET(CMAKE_FIND_LIBRARY_PREFIXES "lib")
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".so" ".a")

and then write

find_library(
    magick
    CORE_RL_magick_ (or NAMES if there are multiple names for the same library on different systems)

    PATHS
      path1
      path2
    ...
    (other options that are specified in documentation and would be usefull to you)
)

EDIT:

CMAKE_FIND_LIBRARY_PREFIXES and CMAKE_FIND_LIBRARY_SUFIXES are set automatically by project() command so calling it first and find_library() after that point is a better solution than setting the variables manually.



回答2:

Why not use find_file() instead of find_library() if you want to find a .dll.



标签: cmake