2

I am working on CMake for generating the visual studio files. I would like to add the xyz.dll in the same folder where abc.exe is residing.

I had read somewhere that when i manually copy the xyz.dll file into the same folder where abc.exe is residing then the problem would be solved. But every time it is not possible.. I want to write the CMake command so that it will find the xyz.dll file and copy into the same folder where the abc.exe is residing..

Following mentioned are the paths where the .exe and .dll file is residing in my PC.

  ${MyWorkSpace_ROOT_DIR}/algoCommon/pthread/dll/xyz.dll
  ${MyWorkSpace_ROOT_DIR}/xml/addAlgo/.../cmakeOut.VS12/Debug/abc.exe

abc is my Project and i would like to confirm whether the following mentioned is wrong or not.

add_custom_command(TARGET abc PRE_BUILD 
                   COMMAND ${CMAKE_COMMAND} -E copy_if_different
                   "${MyWorkSpace_ROOT_DIR}/algoCommon/pthread/dll"              
$<{MyWorkSpace_ROOT_DIR}/xml/addAlgo/.../cmakeOut.VS12/Debug/:abc>)

If this is wrong kindly correct me. If it is correct then I would like to ask few doubts.. will this command automatically copies the xyz.dll files into the folder abc.exe is residing or something else is happening here??

usr1234567
  • 21,601
  • 16
  • 108
  • 128
hightides1973
  • 497
  • 1
  • 9
  • 27
  • 1
    `$<{MyWorkSpace_ROOT_DIR}...>` line is **invalid** [generator expression](https://cmake.org/cmake/help/v3.0/manual/cmake-generator-expressions.7.html). Probably, you want `$` instead (which means directory where exectuable file will be located). – Tsyvarev Jan 13 '16 at 12:04
  • Does this answer your question? [How to copy DLL files into the same folder as the executable using CMake?](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) – 273K Mar 26 '23 at 19:13

2 Answers2

9

As Tsyvarev already commented - the destination expression is invalid. In addition, your source line is incomplete (until you want to copy the whole folder which needs another command)

The right command would be

add_custom_command(TARGET abc POST_BUILD 
               COMMAND ${CMAKE_COMMAND} -E copy_if_different
               "${MyWorkSpace_ROOT_DIR}/algoCommon/pthread/dll/xyz.dll"              
                $<TARGET_FILE_DIR:abc>)

in case you're building also the dll via cmake and you know the target name you could write

add_custom_command(TARGET abc POST_BUILD 
               COMMAND ${CMAKE_COMMAND} -E copy_if_different
               $<TARGET_FILE:xyz>              
               $<TARGET_FILE_DIR:abc>)

where xyz is the target name of the dll

you might also have a look on this one: How to copy DLL files into the same folder as the executable using CMake?

Community
  • 1
  • 1
jenseb
  • 1,593
  • 1
  • 10
  • 13
0

An addition to the answer above, as I needed to copy not only the target .dlls, but also all of its dependencies.

As of CMake 3.21 there's a generator pattern that works specifically for .dll targets and takes all the dependencies as well. Do note, that this only works on DLL platforms as per the documentation. Use it like so:

find_package(foo CONFIG REQUIRED) # package generated by install(EXPORT)

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
)

Link to the official CMake TARGET_RUNTIME_DLLS documentation:

https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html#genex:TARGET_RUNTIME_DLLS
divinas
  • 1,787
  • 12
  • 12