find_library won't find DLLs

I’m trying to write a FindGigeSim.cmake module that creates imported targets for a 3rd party library.

I’m having trouble getting it to work on Windows. Specifically, the following command fails to find the gigesimsdk64.dll file:

find_library(
    GigeSim_LIBRARY
    NAMES gigesimsdk64
    PATHS "C:/Program Files/GigESim/Bin"
          "/usr/lib/GigeSimSDK"
)

I found the command fails because the CMAKE_FIND_LIBRARY_SUFFIXES variable only contains ".lib" and not ".dll".

I’ve read other posts (like this one) which state this is by design, since on Windows, you link to the import-lib instead of the DLL directly.

However, I still need the DLL path to set the IMPORTED_LOCATION property on the imported target, correct? I suppose I could add a separate call to find_path that looks exclusively for the .dll; or I can temporarily add .dll to the CMAKE_FIND_LIBRARY_SUFFIXES, but I wonder if there is a more elegant way to solve this?

I would also like to point out the documentation for CMAKE_FIND_LIBRARY_SUFFIXES:

On Windows systems this is typically .lib and .dll

So part of my confusion is that the docs state that .dll should be present.

FWIW, I’m using Cmake 3.21.2 on Windows 10 with the Visual Studio 2019 generator. My minimum required version is set to 3.10.

Thanks in advance for your help!

Looks like the documentation is bad. I think you would have to use find_file to find a .dll. As you can’t actually link to a .dll find_library will not find it.

1 Like

Yes, find_file is what you probably need for the .dll file. The .lib should be set as the IMPORTED_IMPLIB property by the way.

1 Like

Thanks for the replies! I’ll update my script to use find_file instead.

As an aside, I was curious how DLLs are located from the find modules that ship with CMake. It appears that there isn’t a common pattern for doing this. I only found 3 cases:

  1. FindGSL.cmake assumes the DLL is in the same folder as the lib, so it just does a string replace “.lib” → “.dll”
  2. FindGTest.cmake just sets the IMPORTED_IMPLIB property, and ignores the IMPORTED_LOCATION entirely.
  3. FindHDF5.cmake… I honestly have trouble following this one, but it appears to set IMPORTED_LOCATION and IMPORTED_IMPLIB to the same *.lib path, ignoring the DLL entirely.

Thanks again for you help!

Yeah, it’s usually been “as problems are found, find modules are updated”. It’s really hard to find “perfect” find modules in CMake itself due to the history they tend to have.