3

I need to copy

C:\opencv-3.4.0.-opencl\bin\Debug\*.dll => myproj\build\bin\Debug\*.dll

and also

C:\opencv-3.4.0.-opencl\bin\Release\*.dll => myproj\build\bin\Release\*.dll

I'd like to do it in one command for Build/Release if possible.

Stepan Yakovenko
  • 8,670
  • 28
  • 113
  • 206
  • Thanx, missprint fixed. – Stepan Yakovenko Feb 22 '18 at 22:38
  • I guess a post-build custom command ([doc](https://cmake.org/cmake/help/v3.0/command/add_custom_command.html)) is one way to go. – Dan Mašek Feb 22 '18 at 23:35
  • If the goal is to be able to debug without having to manually copy the libs, I took a slightly different approach. I have a function that calls `CONFIGURE_FILE` to generate `${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.vcxproj.user` that sets up debugging properties, such as command arguments, working directory and environment. For environment, it prepends the location of the DLLs to the `PATH` variable. – Dan Mašek Feb 22 '18 at 23:42
  • 2
    Possible duplicate of [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) – Florian Feb 23 '18 at 09:22
  • 1
    What is wrong with approaches from [that answer](https://stackoverflow.com/a/10672739/3440745) to the question which is suggested as duplicate? – Tsyvarev Aug 24 '20 at 00:45
  • I´d recommend setting dll-location in PATH-command locally. While OpenCV-dlls are small in size, many are not, and one typically has dozens of myprojs, that employ the same dlls. – mainactual Aug 25 '20 at 08:45

2 Answers2

2

You can copy files on a post-build command. A step through tutorial can be found here.

The basic concept is that you can use batch file commands, as a post-build step in Visual Studio to do basically anything you want as you build.

A further tutorial can be found here

For CMAKE

The easiest way is to follow the advice above but instead of putting it in the post-build options in VS just add a custom command

GPPK
  • 6,546
  • 4
  • 32
  • 57
  • 1
    IMHO the question is about using CMake to configure this automagically, rather than having to click through the MSVS GUI every time you generate a new solution. Nothing in your answer, or the tutorials you link to addresses this. – Dan Mašek Feb 23 '18 at 16:48
  • That's true, I say the VisualC++ tag and assumed visual studio. Ill add a cmake solution – GPPK Feb 23 '18 at 19:46
0

You can try using CPack to handle multiple configuration at one go. See an example in the following tutorial

https://cmake.org/cmake/help/latest/guide/tutorial/index.html#packaging-debug-and-release-step-12

By default, CMake’s model is that a build directory only contains a single configuration, be it Debug, Release, MinSizeRel, or RelWithDebInfo. It is possible, however, to setup CPack to bundle multiple build directories and construct a package that contains multiple configurations of the same project.

Then you will need to use either of the following method for each configuration to copy the files you need

configure_file
https://cmake.org/cmake/help/latest/command/configure_file.html

or

add_custom_command

https://cmake.org/cmake/help/latest/command/add_custom_command.html

Here is an example from reddit

https://www.reddit.com/r/cmake/comments/gmewhu/copy_one_file_in_src_directory_to_build_directory/

# Copy <filename> to build directory
set(copy_source_dir "${CMAKE_SOURCE_DIR}/src/<path>")
set(copy_dest_dir "${CMAKE_BINARY_DIR}/Build/<path>/$<CONFIG>")
set(copy_file_name "<filename>")
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory ${copy_dest_dir}
)
add_custom_command(
    TARGET ${PROJECT_NAME} POST_BUILD 
    COMMAND ${CMAKE_COMMAND} -E copy "${copy_source_dir}/${copy_file_name}" "${copy_dest_dir}/${copy_file_name}"
    COMMENT "Copying ${copy_file_name} to build directory"
)
K4M
  • 1,030
  • 3
  • 11