Building of executable and shared library with cmake, runtimelinker does not find dll Building of executable and shared library with cmake, runtimelinker does not find dll windows windows

Building of executable and shared library with cmake, runtimelinker does not find dll


Your problem lies not with linker or compiler, but with the way Windows searches for DLL's.

The OS will use the following algorithm to locate the required DLL's:

Look in:

  1. The directories listed in the Application-specific Path registry key;
  2. The directory where the executable module for the current process is located;
  3. The current directory;
  4. The Windows system directory;
  5. The Windows directory;
  6. The directories listed in the PATH environment variable;

Thus you have two reasonable options if you don't want to clutter the OS directories with your app-specific dll:

  1. Create an app-specific Path registry entry (I would go with this option);
  2. Put your DLL in the same folder as your EXE;
  3. Modify the PATH variable (but why would you do that, if you can go with option 1?);


A solution I prefer that hasn't really been mentioned, is build your shared-libs into the same directory as your executables. This tends to be a much simpler solution.

One way to do this with cmake is

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

Or you can also set output directories based on build flavours.

See how do I make cmake output into a 'bin' dir?


I discovered (what I believe to be) quite a nice way of handling this. It follows the approach of adding the .dll to the same directory as the .exe. You can do it in CMake like so:

if (WIN32)# copy the .dll file to the same folder as the executableadd_custom_command(    TARGET <app-target> POST_BUILD    COMMAND ${CMAKE_COMMAND} -E copy_directory    $<TARGET_FILE_DIR:<lib-target>>    $<TARGET_FILE_DIR:<app-target>)endif()

where app-target is the name of the application or library you're building (created through add_executable or add_library) and lib-target is the imported library brought in with find_package.

# full examplecmake_minimum_required(VERSION 3.14)project(my-app-project VERSION 0.0.1 LANGUAGES CXX)find_package(useful-library REQUIRED)add_executable(my-application main.cpp)target_link_libraries(my-application PUBLIC useful-library::useful-library)if (WIN32)# copy the .dll file to the same folder as the executableadd_custom_command(    TARGET my-application POST_BUILD    COMMAND ${CMAKE_COMMAND} -E copy_directory    $<TARGET_FILE_DIR:useful-library::useful-library>    $<TARGET_FILE_DIR:my-application>)endif()