# Copying dependent DLLs to executable directory?

**URL:** https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852
**Category:** Usage
**Created:** [March 24, 2020, 2:13pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852 "2020-03-24T14:13:48Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [March 24, 2020, 2:13pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/1 "2020-03-24T14:13:48Z")

</div>

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:

```cmake
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?)

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 24, 2020, 4:41pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/2 "2020-03-24T16:41:50Z")

</div>

CMake 3.16 has [`file(GET_RUNTIME_DEPENDENCIES)`](https://cmake.org/cmake/help/latest/command/file.html#get-runtime-dependencies) that you can use for this.

---

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [April 7, 2020, 4:12pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/3 "2020-04-07T16:12:11Z")

</div>

Looking at that it looks to be used along with `install` which is then used with a separate install comment to install it to the system.

I was looking for a build-time command/script that would copy dependencies to the build output directory so when I run a target the necessary dependency is there along with the executable.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 7, 2020, 5:11pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/4 "2020-04-07T17:11:28Z")

</div>

You can add a custom command to run `cmake -P get_runtime_deps.cmake` at build time rather than `install(CODE)` or `install(SCRIPT)` for install time.

---

<div class="post-metadata">

### Author: ![alex](https://discourse.cmake.org/user_avatar/discourse.cmake.org/alex/32/125_2.png) [@alex](https://discourse.cmake.org/u/alex)
#### Post date: [April 15, 2020, 8:08am UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/5 "2020-04-15T08:08:39Z")

</div>

And what if you’re stuck on 3.14? What’s the right way to place DLL dependencies for tests (with CTest)?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 15, 2020, 1:32pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/6 "2020-04-15T13:32:18Z")

</div>

You could try `GetPrerequisites`, but it’s finicky (though probably better on Windows due to the simpler linking model). If this is a necessary thing, I recommend just bumping your minimum.

---

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [April 19, 2020, 11:34am UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/7 "2020-04-19T11:34:47Z")

</div>

> [@ben.boeckel](#):
>
> You can add a custom command to run `cmake -P get_runtime_deps.cmake` at build time

Is `get_runtime_deps.cmake` something that exists? Or a CMake script I have to write myself? I’m still not clear on how this allows me to get the dependencies of a binary in a build command.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 19, 2020, 12:25pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/8 "2020-04-19T12:25:08Z")

</div>

You have to write that script. It should call the `file(GET_RUNTIME_DEPENDENCIES)` command I linked above.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [May 5, 2020, 5:30pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/9 "2020-05-05T17:30:39Z")

</div>

I have a lot of trouble with get\_prerequisites(). One of the problems is that it returns prerequisite dlls without their path on windows. Your cmake code then needs to take care of finding the libraries. You could now remember all the dlls found during configure and take those. This does not work with if a dll is used by the prerequisite but not needed at link time. You can then try to remember the paths of the dlls found in the configure phase and look for a prerequisites there. This may help but is still not garanteed to work if the “lower level dll” is located elsewhere.  
Yesterday I had another problem with get\_prerequisites() which I posted today.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [May 7, 2020, 3:28pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/10 "2020-05-07T15:28:39Z")

</div>

I recommend not using `get_prerequisites` anymore. `file(GET_RUNTIME_DEPENDENCIES)` is a better implementation of that logic. There are many corner cases `get_prerequisites` does not consider and fixing it is not really feasible (it would basically involve writing the dynamic loader logic for each platform in CMake…which is incidentally why it behaves so poorly today).

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [May 7, 2020, 3:36pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/11 "2020-05-07T15:36:03Z")

</div>

Thanks for the hint. I already saw that GET\_RUNTIME\_DEPENDENCIES has been added in a recent cmake version.  
Do you know if it is also faster than get\_prerequisites(). The latter is terribly slooooooow.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [May 7, 2020, 3:41pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/12 "2020-05-07T15:41:22Z")

</div>

It is indeed faster, since it’s written in native C++ 🙂

It is also more accurate. We painstakingly drew out the flowchart of dependency searching for Windows, Linux, and macOS and mimicked it as best as we could. `GetPrerequisites` was not so rigorous.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [May 7, 2020, 5:54pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/13 "2020-05-07T17:54:56Z")

</div>

Hello Kyle,  
sounds superb. Thanks so much for providing this new functionality. Does the windows version now provide absolute paths to the dependencies as on linux? This would make my day even more and might make me code over night!  
Best regards, Stephan.

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [May 7, 2020, 6:01pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/14 "2020-05-07T18:01:40Z")

</div>

Yes, all resolved dependencies are returned as absolute paths.

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [May 7, 2020, 6:04pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/15 "2020-05-07T18:04:39Z")

</div>

GREAT! Just started downloading 3.17.2 …  
Thanks so much again. I hope I can contribute to cmake some time in other ways than asking stupid questions:)

---

<div class="post-metadata">

### Author: ![thomthom](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thomthom/32/141_2.png) [@thomthom](https://discourse.cmake.org/u/thomthom)
#### Post date: [May 23, 2020, 4:12pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/16 "2020-05-23T16:12:38Z")

</div>

> [@ben.boeckel](#):
>
> You have to write that script. It should call the `file(GET_RUNTIME_DEPENDENCIES)` command I linked above.

Revisting this again. This `.cmake` file to run at build time, how will that know what file to be checking? Do I need to pass the path to the binary to check dependency for as a command line parameter? If so, how do I then obtain the path where the binary for a target will be?

---

<div class="post-metadata">

### Author: ![Stephan268](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@Stephan268](https://discourse.cmake.org/u/Stephan268)
#### Post date: [May 25, 2020, 10:21am UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/17 "2020-05-25T10:21:33Z")

</div>

Your CMakeLists.txt specifies what library or executable is being built and where it is going to be located. This is the place where you can add this information to “some variable”, e.g. a list of all libraries/executables that you are building. Towards the end of the CMakeLists.txt (i.e. after all libraries/executables are defined) you can advise cmake to call installation code (using install(CODE …)) at install time. This code would then evaluate the content of the “variable” and gather all depencies using file(GET\_RUNTIME\_DEPENCIES …). Works for me although I do not like my “global variable”. I also have problems with file (INSTALL …) and symbolic links on windows, but this is another story.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 26, 2020, 12:50pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/18 "2020-05-26T12:50:43Z")

</div>

You might be able to avoid the global variable by using `file(GENERATE)` to create scripts to call with `install(SCRIPT)`. Using `file(GENERATE)`, you can use things like generator expressions that provide the locations of build executables and libraries.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [October 29, 2021, 3:09pm UTC](https://discourse.cmake.org/t/copying-dependent-dlls-to-executable-directory/852/19 "2021-10-29T15:09:15Z")

</div>

CMake 3.21 introduced [TARGET\_RUNTIME\_DLLS](https://cmake.org/cmake/help/git-stage/manual/cmake-generator-expressions.7.html#genex:TARGET_RUNTIME_DLLS) generator expression that can also help.
