116

We use CMake for generating the Visual Studio files of our sources in our SVN. Now my tool requires some DLL files to be in the same folder as the executable. The DLL files are in a folder alongside the source.

How can I change my CMakeLists.txt such that the generated Visual Studio project will either have already the particular DLL files in the release/debug folders or will copy them upon compilation?

Smi
  • 13,151
  • 9
  • 55
  • 63
Mat
  • 4,139
  • 9
  • 43
  • 66
  • 1
    [CMake 3.21 has a new handy generator expr for this use case.](https://stackoverflow.com/a/69736197/4594532). – Gnimuc Oct 27 '21 at 09:45

11 Answers11

144

I'd use add_custom_command to achieve this along with cmake -E copy_if_different.... For full info run

cmake --help-command add_custom_command
cmake -E


So in your case, if you have the following directory structure:

/CMakeLists.txt
/src
/libs/test.dll

and your CMake target to which the command applies is MyTest, then you could add the following to your CMakeLists.txt:

add_custom_command(TARGET MyTest POST_BUILD        # Adds a post-build event to MyTest
    COMMAND ${CMAKE_COMMAND} -E copy_if_different  # which executes "cmake - E copy_if_different..."
        "${PROJECT_SOURCE_DIR}/libs/test.dll"      # <--this is in-file
        $<TARGET_FILE_DIR:MyTest>)                 # <--this is out-file path


If you just want the entire contents of the /libs/ directory copied, use cmake -E copy_directory:

add_custom_command(TARGET MyTest POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/libs"
        $<TARGET_FILE_DIR:MyTest>)


If you need to copy different dlls depending upon the configuration (Release, Debug, eg) then you could have these in subdirectories named with the corresponding configuration: /libs/Release, and /libs/Debug. You then need to inject the configuration type into the path to the dll in the add_custom_command call, like this:

add_custom_command(TARGET MyTest POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/libs/$<CONFIGURATION>"
        $<TARGET_FILE_DIR:MyTest>)
Fraser
  • 70,437
  • 17
  • 232
  • 212
  • 5
    Quick note for what worked in my case in case it helps someone else in the future: I have a static library project which the main executable optionally links against, and that library requires a DLL to be copied if added. So in that library's CMakeLists.txt file I used `${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$` for the target destination. Otherwise it would copy it to the library build path, which was useless. – AberrantWolf Sep 22 '16 at 02:00
  • 1
    isn't it the goal of the cmake `install()` directives to assemble the binaries? Or maybe cmake `LIBRARY` things? I don't really know the toolchain. – Sandburg Mar 28 '18 at 15:40
  • 2
    `$` - what is it? How to print info, what exactly mean – ilw Aug 17 '18 at 10:17
  • Unless I missed something when I was integrating the command this method doesn't work for libraries added with ```IMPORTED_LIBRARIES```. It complains about not being able to run a post-build command when nothing has been built. – Tzalumen Aug 22 '18 at 01:00
  • 2
    Upon testing and prodding my cmake: If you are using ```IMPORTED``` libraries, and need to relocate the DLLs, you need to use a variant command. If you have added your ```IMPORTED``` library as ```MyImportedLib``` you would use ```COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $``` Note that to run multiple post build commands you need them all bound into one custom command e.g. ```add_custom_command(TARGET MyTest POST_BUILD COMMAND #your first command# COMMAND #Your second command#)``` – Tzalumen Aug 22 '18 at 15:28
  • Thanks, however do you have to specify a TARGET? – Jesper.Lindberg Apr 09 '20 at 07:21
  • @Jesper.Lindberg You _want_ to specify a target, because you're appending these commands to that target's build "script". IOW, the commands depend on `MyTest`, and can't be run until it's completed building (hence the "`POST_BUILD`"). So setting the commands as `TARGET MyTest POST_BUILD` tells CMake to wait until it's done building `MyTest` to run them. – FeRD Jan 08 '21 at 04:11
29

I put these lines in my top-level CMakeLists.txt file. All the libraries and executables compiled by CMake will be placed in the top level of the build directory so that the executables can find the libraries and it is easy to run everything.

set (CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

Note that this doesn't solve the OP's problem of copying precompiled binaries from the project's source directory.

David Grayson
  • 79,096
  • 23
  • 144
  • 182
19

I've had this problem today when tried to make a Windows build of my program. And I ended up doing some research myself since all these answers didn't satisfy me. There were three main issues:

  • I wanted debug builds to be linked with debug versions of libraries and release builds to be linked with release builds of libraries, respectively.

  • In addition to that, I wanted correct versions of DLL files (Debug/Release) to be copied to output directories.

  • And I wanted to achieve all this without writing complex and fragile scripts.

After browsing some CMake manuals and some multiplatform projects at github I've found this solution:

Declare your library as a target with "IMPORTED" attribute, reference its debug and release .lib and .dll files.

add_library(sdl2 SHARED IMPORTED GLOBAL)
set_property(TARGET sdl2 PROPERTY IMPORTED_IMPLIB_RELEASE "${SDL_ROOT_PATH}/lib/SDL2.lib")
set_property(TARGET sdl2 PROPERTY IMPORTED_LOCATION_RELEASE "${SDL_ROOT_PATH}/bin/SDL2.dll")
set_property(TARGET sdl2 PROPERTY IMPORTED_IMPLIB_DEBUG "${SDL_ROOT_PATH}/lib/SDL2d.lib")
set_property(TARGET sdl2 PROPERTY IMPORTED_LOCATION_DEBUG "${SDL_ROOT_PATH}/bin/SDL2d.dll")

Link this target with your project as usual

target_link_libraries(YourProg sdl2 ...)

Make custom build step to copy dll file to its destination if it has been altered somehow since previous build

add_custom_command ( TARGET YourProg POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
    $<TARGET_FILE:sdl2> $<TARGET_FILE_DIR:YourProg>
)
Minor Threat
  • 1,955
  • 1
  • 16
  • 31
  • I've been struggling to understand the usefulness of generator expressions for a long time, as well as trying to figure out a way to do this reliably. This solved both problems for me at once, and should probably be the accepted answer. – Jesse Wyatt Apr 06 '22 at 08:31
6

Moving files during build using install

I had this issue trying to follow the CMake official tutorial on Step 9. This was the location of the file I wanted to move:

src
 |_build
    |_Debug
       - `MathFunctions.dll`

This was the location I wanted the file to be in:

src
 |_build
    |_install
         |_bin
             - `MathFunctions.dll`

Since this DLL was generated as a shared library, all I did was to include this line in the CMakeLists.txt in the subdirectory that contained the source code for the library src/Mathfunctions/CMakeLists.txt

install(FILES ${PROJECT_BINARY_DIR}/$<CONFIG>/MathFunctions.dll
DESTINATION bin)

Thanks to your answers I could think on this one. Is just one line, so I think is ok. The $<CONFIG> can have two values Debug or Release Depending on how the project is built, as the original question required.

6

For Windows users, there is a new generator expression $<TARGET_RUNTIME_DLLS:tgt> in CMake 3.21+ and you could use this official snippet for copying all of the DLLs that a target depends on.

find_package(foo REQUIRED)

add_executable(exe main.c)
target_link_libraries(exe PRIVATE foo::foo foo::bar)
add_custom_command(TARGET exe POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:exe> $<TARGET_FILE_DIR:exe>
  COMMAND_EXPAND_LISTS
  )
Gnimuc
  • 7,448
  • 2
  • 37
  • 48
5

An addendum to the accepted answer, added as a separate answer so I get code formatting:

If you are building your dlls in the same project, they will usually be in Release, Debug, etc. directories. You'll have to use the Visual Studio environment variables to correctly copy them. e.g.:

"${PROJECT_BINARY_DIR}/your_library/\$\(Configuration\)/your_library.dll"

for the source and

"${CMAKE_CURRENT_BINARY_DIR}/\$\(Configuration\)/your_library.dll"

for the destination. Note the escaping!

You can't use the CMake CMAKE_BUILD_TYPE variable for the configuration since it's resolved at VS project generation time and will always be whatever the default is.

Dana Robinson
  • 4,176
  • 8
  • 30
  • 41
5

You can also use the command find_library:

find_library(<some_var> NAMES <name_of_lib> PATHS "<path/to/lib>")

With a defined EXECUTABLE_PATH, for instance:

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

you could move the .dll files that your executable need, with

file(COPY ${<some_var>}
    DESTINATION ${EXECUTABLE_OUTPUT_PATH})
Felipe Thorn
  • 104
  • 1
  • 4
2

This is useful for one of them

SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
    ${PROJECT_SOURCE_DIR}/lib CACHE
    PATH "Directory where all the .lib files are dumped." FORCE)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    ${PROJECT_SOURCE_DIR}/bin CACHE
    PATH "Directory where .exe and .dll files are dumped." FORCE)
1

You probably need to add custom target and make it depend on one of your executable targets.

To copy file using above function use:

COMMAND ${CMAKE_PROGRAM} -E copy_if_different ${CMAKE_BINARY_DIR}/path/to/file.dll ${CMAKE_BINARY_DIR}/where/to/put/file.dll`
genpfault
  • 49,394
  • 10
  • 79
  • 128
arrowd
  • 31,833
  • 7
  • 76
  • 102
1

The following command from the currently top rated answer depends on the output being put in /libs/.

add_custom_command(TARGET MyTest POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${PROJECT_SOURCE_DIR}/libs/$<CONFIGURATION>"
        $<TARGET_FILE_DIR:MyTest>)

I use the below command to do the copy, which works for me everywhere. Note that I'm only copying the output dll here, not the entire directory. Also note that I'm hard coding a destination /bin/ directory, which is specific to this project. But I wanted to share the $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/$<TARGET_FILE_NAME:${CMAKE_PROJECT_NAME}> syntax, which I think is neat:

add_custom_command(TARGET ${CMAKE_PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different
        $<TARGET_FILE_DIR:${CMAKE_PROJECT_NAME}>/$<TARGET_FILE_NAME:${CMAKE_PROJECT_NAME}>
        ${CMAKE_CURRENT_SOURCE_DIR}/bin/$<TARGET_FILE_NAME:${CMAKE_PROJECT_NAME}>)
Torkel
  • 78
  • 6
0

I'm a CMake beginner, but still I wanted to shared my experience. In my case I needed a post-install copy so that all my binaries are in. In the case of third-party binary that can be imported within CMake, the following works for me:

find_package( dependency REQUIRED )
if( MSVC ) 
    # If done properly and if the dependency has a correct config file, IMPORTED_LOCATION_RELEASE should be defined
    get_target_property( DEP_SHARED_LIB_PATH dependency IMPORTED_LOCATION_RELEASE )
    # Create a bin directory in the install folder
    add_custom_command(TARGET BGS POST_BUILD COMMAND ${CMAKE_COMMAND} -E make_directory  ${CMAKE_INSTALL_PREFIX}/bin/)
    # Copy the shared lib file
    add_custom_command(TARGET BGS POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${DEP_SHARED_LIB_PATH} ${CMAKE_INSTALL_PREFIX}/bin/)
endif()

Obviously IMPORTED_LOCATION_RELEASE can have variants depending on how the shared library was built / installed. Could be IMPORTED_LOCATION_DEBUG.

Maybe there's a better way to get that property name, I don't know.

Norman Pellet
  • 354
  • 2
  • 9