1

I don't know how to configure multiple build types in Visual Studio with Cmake. For example, when debug is selected in Visual Studio, I need to copy *d.dll to ${CMAKE_BINARY_DIR}/rundir/debug/bin and when release is selected in Visual Studio, I need to copy *.dll to ${CMAKE_BINARY_DIR}/rundir/release/bin.

Can someone tell me how to do that?

Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
Cosi10a1
  • 41
  • 1
  • 2
  • Do you mean _copy_ in the sense of a _post-success-build_ or as an actual _install_ step? You know, you can use generator expressions to specify a build-type-dependent location? – Torbjörn Aug 25 '16 at 04:27
  • Is the DLL the product of another target? – Torbjörn Aug 25 '16 at 06:09
  • .I found the solution. I use $(configuration) variable of visual studio in cmake like this: add_custom_command(TARGET obs POST_BUILD COMMAND if $(ConfigurationName) == Debug ( xcopy /y /d "$(QTDIR)\\bin\\Qt5WebKitWidgetsd.dll" "$(SolutionDir)rundir\\$(Configuration)\\bin\\64bit" \n ) else (//DO something) – Cosi10a1 Aug 26 '16 at 04:21
  • You could add/edit that to your question to improve it a little bit. There is only one problem with your solution: It isn't platform independent and VS-/MSBuild-centric. I've posted an answer using CMake's platform abstraction. – Torbjörn Aug 26 '16 at 04:54
  • Does this answer your question? [Copy target file to another location in a post build step in CMake](https://stackoverflow.com/questions/9994045/copy-target-file-to-another-location-in-a-post-build-step-in-cmake) – starball Sep 14 '22 at 05:15

2 Answers2

3

As you want a post-build action, there is a CMake command and also the appropriate variables available.

The platform independent command to copy around files with CMake is using CMake itself on the command line:

${CMAKE_COMMAND} -E copy_if_different "${src}" "${dest}"

The "current" configuration can be extracted with generator expressions:

$<CONFIG>

and even directly tested for trueness

$<CONFIG:Debug>

The output directory for a target's binaries (i.e. executables and shared libraries/DLLs) is given with the target property RUNTIME_OUTPUT_DIRECTORY (and RUNTIME_OUTPUT_DIRECTORY_<CONFIG>) which are pre-populated with the global variable CMAKE_RUNTIME_OUTPUT_DIRECTORY (and CMAKE_RUNTIME_OUTPUT_DIRECTORY_<CONFIG>).

Finally, we can compose the post-build command

add_custom_command(TARGET myTarget POST_BUILD
                   COMMAND if $<CONFIG:Debug> ("${CMAKE_COMMAND}" -E copy_if_different "${path_to_dependent_dll}/dependent.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG}")
                   COMMAND if $<CONFIG:Release> ("${CMAKE_COMMAND}" -E copy_if_different "${path_to_dependent_dll}/dependent.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE}")
                   COMMENT "Copying dependent DLL"
)
Torbjörn
  • 5,512
  • 7
  • 46
  • 73
2

The previous answer was very usefull for me, but it didn't work for me so I maid these modifications to make it work:

  • I replaced if $<CONFIG:Debug> by if $<CONFIG:Debug> neq 0, the same for if $<CONFIG:Release>
  • ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG} and ${CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE} where not defined in my case, so I replaced them by $<TARGET_FILE_DIR:myTarget>