How to copy dlls to binary directory

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:

set(wxWidgets_USE_STATIC ON)
set(wxWidgets_USE_UNICODE ON)

find_package(wxWidgets REQUIRED gl core base)


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?

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

function (copy_dlls target)
    if (WIN32)
        add_custom_command (
            TARGET "${target}" POST_BUILD
            COMMAND "${CMAKE_COMMAND}" -E copy -t "$<TARGET_FILE_DIR:${target}>"
    endif ()
endfunction ()
1 Like

You might want to look at these commands:


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.