Copying dependent DLLs to executable directory?

I got a project where I use a third party library (Windows).

  • A.lib
  • A.dll
  • B.dll

B.dll is a dependency of A.dll not exposed to the A.dll interface.

I’m setting up an imported library:

add_library(Example::LibA SHARED IMPORTED)
set_target_properties(Example::LibA PROPERTIES
  IMPORTED_LOCATION "${LibA_LIBRARY}"
  IMPORTED_IMPLIB "${LibA_LIBRARY}"
  INTERFACE_INCLUDE_DIRECTORIES "${LibA_INCLUDE_DIR}"
)

I’m looking for a way to have both the DLLs copied to the executable’s output directory (unit tests executable). I was hoping for a way to define this as part of the interface to the imported library so consumers only need to link to the imported target.

Is there any such way?

If not, what would be the CMake way for this? Set up a POST build event that copies the DLLs per executable target that links to it? (How would you then obtain the paths for the DLL from the importer target?)

CMake 3.16 has file(GET_RUNTIME_DEPENDENCIES) that you can use for this.

1 Like