15

One specifies find_library(<var> 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 or CORE_RL_magick does not work.

2
  • Changing the file extension to lib enables cmake to identify it... What is the difference between dll and lib on windows? Commented Jan 9, 2013 at 18:10
  • This should be it's own question if you can't find it already on SO, but short answer is that the linker uses the .lib, and the .exe uses the .dll. Commented May 2, 2018 at 0:52

2 Answers 2

24

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If you want to link to it, you will need the .lib files. On Windows they are called "import library" and basically tell your linker about the exported symbols of your .dll. During execution, you will then (only) need the .dll
Additionally, be extra cautious about CMAKE_FIND_LIBRARY_PREFIXES and CMAKE_FIND_LIBRARY_SUFFIXES! I've just stumbled into the library, which have set suffixes to ".dll.a" and I've been completely clueless about why find_library could not find any combination of "lib", "lib*.lib" and "*.a" files.
Also remember that Windows library vendors know Windows is case insensitive and like to provide libs in ALL CAPS, where cmake is case sensitive. I had to use `SET( CMAKE_FIND_LIBRARY_SUFFIXES ".lib" ".LIB" ".dll" ".DLL" ) to find all my libs.
1

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

1 Comment

Because I want to find the library on Linux and MS Windows.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.