[CMake] Copying DLLs to output directory

Hendrik Sattler post at hendrik-sattler.de
Fri Oct 31 15:53:33 EDT 2014



Am 31. Oktober 2014 20:28:00 MEZ, schrieb Michael Jackson <mike.jackson at bluequartz.net>:
>It sucks, but I do that with Qt's libraries. One target for each
>library. I prefix the target with "ZZ_" so that in IDEs like Visual
>Studio and Xcode those targets fall to the bottom of the list. I also
>group them in folders if Visual Studio will allow it. I use the "copy
>if different" argument to the CMake command for the copy. Seems to
>work.

Hmm, imported targets don't appear in VS. So why would you name them in such a way?

>Mike Jackson
>
>On Oct 31, 2014, at 3:11 PM, Robert Dailey <rcdailey.lists at gmail.com>
>wrote:
>
>> I like this idea but it doesn't seem like it will work for targets
>> with multiple DLLs... for example boost. It has several DLLs. I don't
>> want to define 1 target for each DLL either. Sometimes that doesn't
>> make sense.
>> 
>> On Wed, Oct 29, 2014 at 10:45 AM, Hendrk Sattler
>> <post at hendrik-sattler.de> wrote:
>>> Am 2014-10-28 18:25, schrieb Robert Dailey:
>>>> 
>>>> I have a third party library like OpenSSL prebuilt for each
>platform
>>>> and in my own structure in version control. I have a CMake script
>that
>>>> creates an INTERFACE library target for it. I setup the include
>>>> directories and link targets. However, I don't see a way to
>configure
>>>> DLLs in the interface library target. How would you do this, and
>what
>>>> would CMake do to these targets to make sure they are copied to the
>>>> output directory of the executable I run from Visual Studio for
>>>> debugging?
>>> 
>>> 
>>> I have this for ZLib:
>>> if ( ZLIB_FOUND )
>>>  if ( WIN32 )
>>>    get_filename_component( ZLIB_LIBDIR "${ZLIB_LIBRARY}" PATH )
>>>    get_filename_component ( ZLIB_BASENAME "${ZLIB_LIBRARY}" NAME_WE
>)
>>>    get_filename_component ( ZLIB_LIBDIR_BASE "${ZLIB_LIBDIR}" PATH )
>>>    find_file ( ZLIB_DLL
>>>
>"${CMAKE_SHARED_LIBRARY_PREFIX}${ZLIB_BASENAME}${CMAKE_SHARED_LIBRARY_SUFFIX}"
>>>      HINTS
>>>        "${ZLIB_LIBDIR_BASE}"
>>>      PATH_SUFFIXES
>>>        bin
>>>      NO_DEFAULT_PATH
>>>    )
>>>    mark_as_advanced ( ZLIB_DLL )
>>>    if ( ZLIB_DLL )
>>>      add_library ( zlib SHARED IMPORTED GLOBAL )
>>>      set_property ( TARGET zlib PROPERTY IMPORTED_IMPLIB
>"${ZLIB_LIBRARY}"
>>> )
>>>      set_property ( TARGET zlib PROPERTY IMPORTED_LOCATION
>"${ZLIB_DLL}" )
>>>    else ( ZLIB_DLL )
>>>      add_library ( zlib STATIC IMPORTED GLOBAL )
>>>      set_property ( TARGET zlib PROPERTY IMPORTED_LOCATION
>>> "${ZLIB_LIBRARY}" )
>>>    endif ( ZLIB_DLL )
>>>  else( WIN32 )
>>>    add_library ( zlib UNKNOWN IMPORTED GLOBAL )
>>>    set_property ( TARGET zlib PROPERTY IMPORTED_LOCATION
>"${ZLIB_LIBRARY}"
>>> )
>>>  endif( WIN32 )
>>>  set_property ( TARGET zlib PROPERTY INTERFACE_INCLUDE_DIRECTORIES
>>>    "${ZLIB_INCLUDE_DIR}"
>>>  )
>>> 
>>>  set ( ZLIB_LIBRARIES zlib )
>>>  set ( ZLIB_INCLUDE_DIRS "${ZLIB_INCLUDE_DIR}" )
>>> endif ( ZLIB_FOUND )
>>> 
>>> The .lib goes into IMPORTED_IMPLIB and the .dll goes into
>IMPORTED_LOCATION.
>>> The way to find the .dll from the location of the .lib might differ
>for
>>> different libraries.
>>> For ZLib, the base name is the same.
>>> 
>>> Later, you can use this imported target:
>>>  add_custom_command ( TARGET myTarget POST_BUILD
>>>    COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:zlib>
>>> $<TARGET_FILE_DIR:myTarget>
>>>  )
>>> Now, zlib.dll is in the same directory as myTarget.dll.
>>> 
>>> HS
>>> 
>>>> On Tue, Oct 28, 2014 at 12:21 AM, Hendrik Sattler
>>>> <post at hendrik-sattler.de> wrote:
>>>>> 
>>>>> Hi,
>>>>> 
>>>>> you can use generator expression in a post build rule to copy the
>dll
>>>>> file to the same target dir as the target you link it with. The
>easiest way
>>>>> to do this is to properly define all 3rd party libraries as
>imported targets
>>>>> that contains both, the lib and the dll file.
>>>>> Sadly, the FindQt4 on Windows doesn't do this and thus make life
>harder
>>>>> than needed. CMake configuration files should always do this
>right.
>>>>> 
>>>>> OTOH, you could also write a wrapper batch file or change VS
>properties
>>>>> to modify PATH to include all libraries before the regular path.
>>>>> 
>>>>> HS
>>>>> 
>>>>> 
>>>>> Am 28. Oktober 2014 02:55:08 MEZ, schrieb Robert Dailey
>>>>> <rcdailey.lists at gmail.com>:
>>>>>> 
>>>>>> This actually used to be a very difficult problem to solve.
>However,
>>>>>> to debug in visual studio it's essential.
>>>>>> 
>>>>>> If I have DLLs located in third party directories OR from targets
>that
>>>>>> I depend on, those must all be copied to the directory of the
>>>>>> executable I'm debugging in order for those DLLs to be found and
>>>>>> loaded.
>>>>>> 
>>>>>> Using CMake 3.0.2, I hope this task is simpler, especially with
>the
>>>>>> introduction of a nice suite of generator expressions. Can anyone
>>>>>> recommend a good way to do this?
>>>>> 
>>>>> 
>>>> --
>>>> 
>>>> Powered by www.kitware.com
>>>> 
>>>> Please keep messages on-topic and check the CMake FAQ at:
>>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>> 
>>>> Kitware offers various services to support the CMake community. For
>>>> more information on each offering, please visit:
>>>> 
>>>> CMake Support: http://cmake.org/cmake/help/support.html
>>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>> 
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>> 
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://public.kitware.com/mailman/listinfo/cmake
>>> 
>>> --
>>> 
>>> Powered by www.kitware.com
>>> 
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>> 
>>> Kitware offers various services to support the CMake community. For
>more
>>> information on each offering, please visit:
>>> 
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>> 
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>> 
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/cmake
>> -- 
>> 
>> Powered by www.kitware.com
>> 
>> Please keep messages on-topic and check the CMake FAQ at:
>http://www.cmake.org/Wiki/CMake_FAQ
>> 
>> Kitware offers various services to support the CMake community. For
>more information on each offering, please visit:
>> 
>> CMake Support: http://cmake.org/cmake/help/support.html
>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>> 
>> Visit other Kitware open-source projects at
>http://www.kitware.com/opensource/opensource.html
>> 
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/cmake



More information about the CMake mailing list