# How to copy dlls to binary directory

**URL:** https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154
**Category:** Code
**Tags:** os:windows
**Created:** [February 22, 2024, 4:03am UTC](https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154 "2024-02-22T04:03:59Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Saviz](https://discourse.cmake.org/user_avatar/discourse.cmake.org/saviz/32/6194_2.png) [@Saviz](https://discourse.cmake.org/u/Saviz)
#### Post date: [February 22, 2024, 4:03am UTC](https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154/1 "2024-02-22T04:03:59Z")

</div>

I am utilizing the **wxWidgets** library in my projects to develop GUI applications. While I am able to successfully link against the library and run my application within my IDE environment, issues arise when attempting to execute the final **.exe** file independently. The attempt to run the executable will result in numerous “ **DLL not found** ” errors, which is anticipated since the program cannot find the necessary dependencies when trying to run the program. Therefore, I wanted to know if there exists a method to direct CMake to **automatically identify the required DLLs and copy them to the binary directory alongside the executable** , thus eliminating the need for a manual copy operation.

  

Here is my current code for linking against wxWidgets:

  

```auto
set(wxWidgets_USE_STATIC ON)
set(wxWidgets_EXCLUDE_COMMON_LIBRARIES TRUE)
set(wxWidgets_USE_UNICODE ON)

find_package(wxWidgets REQUIRED gl core base)

include(${wxWidgets_USE_FILE})

target_link_libraries(${EXECUTABLE_NAME} PRIVATE ${wxWidgets_LIBRARIES})

```

  

**PS:** despite the fact that I have the setting `wxWidgets_USE_STATIC` set to `ON`, the library just refuses to link statically and always defaults to dynamic linking. Any idea why this is?

---

<div class="post-metadata">

### Author: ![benthevining](https://discourse.cmake.org/user_avatar/discourse.cmake.org/benthevining/32/1924_2.png) [@benthevining](https://discourse.cmake.org/u/benthevining)
#### Post date: [February 22, 2024, 4:26am UTC](https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154/2 "2024-02-22T04:26:16Z")

</div>

Here’s some code I’ve used for that:

```cmake
function (copy_dlls target)
    if (WIN32)
        add_custom_command (
            TARGET "${target}" POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy -t "$<TARGET_FILE_DIR:${target}>"
                    "$<TARGET_RUNTIME_DLLS:${target}>" USES_TERMINAL COMMAND_EXPAND_LISTS
        )
    endif ()
endfunction ()

```

---

<div class="post-metadata">

### Author: ![bill.hoffman](https://discourse.cmake.org/user_avatar/discourse.cmake.org/bill.hoffman/32/13_2.png) [@bill.hoffman](https://discourse.cmake.org/u/bill.hoffman)
#### Post date: [February 22, 2024, 7:21pm UTC](https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154/3 "2024-02-22T19:21:18Z")

</div>

You might want to look at these commands:

[https://cmake.org/cmake/help/latest/command/install.html#runtime-dependency-set](https://cmake.org/cmake/help/latest/command/install.html#runtime-dependency-set)  
[https://cmake.org/cmake/help/latest/command/file.html#get-runtime-dependencies](https://cmake.org/cmake/help/latest/command/file.html#get-runtime-dependencies)

---

<div class="post-metadata">

### Author: ![markdewit\_ies](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/e95f7d/32.png) [@markdewit\_ies](https://discourse.cmake.org/u/markdewit_ies)
#### Post date: [February 29, 2024, 8:10am UTC](https://discourse.cmake.org/t/how-to-copy-dlls-to-binary-directory/10154/4 "2024-02-29T08:10:47Z")

</div>

This is what I do as well - any data or DLL dependencies I’ve wrapped into an install command. With a bit of flexibility you can then use the install command to facilitate this deploying of your content, and you need to run it once after your first build (and whenever your dependencies change), and it still works for CPack as well.
