[CMake] Copying of 3rd party DLLs in a POST-BUILD step

Michael Hertling mhertling at online.de
Mon Jan 9 10:47:24 EST 2012


On 01/09/2012 10:05 AM, Michael Stürmer wrote:
> I have found some topics related to my issue on the web, but none so far helped me to fix it:
>  
> I use Visual Studio 2010 on Windows 7 64Bit.
>  
> During my build, all binaries are collected in one folder, which makes it easier for me to debug the project. But to be able to run the program actually, I have to copy several dlls (like Qt, openCV etc.) into the folder for the program to find them. Putting the libraries in the system path is not an option for me, as I switch between 32- and 64-bit on the same system.
>  
> I managed to locate the folder where the dlls are (using some CMake-Variables) and using a custom command like
>  
> ADD_CUSTOM_COMMAND( TARGET CopyDlls POST_BUILD
>                 COMMAND copy "${DLL_3RD}/*.dll"  "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/$<CONFIGURATION>"
>                 COMMENT "copying dlls ."
>                 )
>  
> This copies ALL dlls from the ${DLL_3RD} folder to the binary folder, for Qt that would be the relase as well as the debug libraries.
>  
> Now my question:
>  
> I would like to only copy those dlls I need, i.e. I have to determine somehow if I'm in debug or release mode and select the appropriate libraries by adding "d" for debug versions. For openCV I need "opencv_core231.dll" in release and "opencv_core231d.dll" in debug mode. Does anyone have a solution/workaround/idea for this problem?
>  
> Best regards,
> Michael

Perhaps, you might perform a temporary installation for debugging
purposes and use the BundleUtilities for this installation only:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8 FATAL_ERROR)
PROJECT(P C)
SET(CMAKE_VERBOSE_MAKEFILE ON)
FILE(WRITE ${CMAKE_BINARY_DIR}/main.c "int main(void){return 0;}\n")
ADD_EXECUTABLE(main main.c)
INSTALL(TARGETS main DESTINATION bin)
INSTALL(CODE "
IF(CMAKE_INSTALL_PREFIX STREQUAL \"${CMAKE_BINARY_DIR}/debuginstall\")
MESSAGE(\"Use BundleUtilities here\")
ENDIF()
")

If you specify ${CMAKE_BINARY_DIR}/debuginstall as CMAKE_INSTALL_PREFIX,
the BundleUtilities are included - if you replace the MESSAGE() command,
of course - and should do the job. Otherwise, i.e. with different CMAKE_
INSTALL_PREFIX, the installation runs as usual. In this way, you do not
need an installation component or the like to have CMake recognize your
special debugging installation. The downside is that you've to rebuild
the project with the final CMAKE_INSTALL_PREFIX when performing the
"real" installation, but usually, you do this anyway in order to
switch from the debug configuration to the release one.

Regards,

Michael


More information about the CMake mailing list