[CMake] Copying imported libs

Marek Vojtko (Firaxis) Marek.Vojtko at firaxis.com
Wed Jul 3 10:44:19 EDT 2013


On Mon, 1 Jul 2013 23:06:04 +0000 (UTC) Gennadiy Rozental wrote:

> I've defined bunch of imported shared library targets for various third
> party libraries I am using. On windows I want to copy dlls into binary
> directory once someone depends on the library. In other words if  have
> imported shared library foo and I have executable or library which links
> with it I want foo.dll somehow get copied into bin directory as post build
> event for the target this library linked to.

The easiest way to copy imported library DLLs next to the executable is to create a list of them in you CMake file (I usually call this list "ExternalSharedLibraries") and then use the FILE(COPY ...) command to copy them where I need them:

list( APPEND ExternalSharedLibraries "<path_to_dll>" )

# Where will the executables be located?
if( DEFINED EXE_DIR )
    set( exeDir "${EXE_DIR}" )
else()
    set( exeDir "${CMAKE_CURRENT_BINARY_DIR}" )
endif()

# This will copy any files with differing timestamps
message( STATUS "\tCopying <name> shared libraries." )
file( COPY ${ExternalSharedLibraries} DESTINATION "${exeDir}" NO_SOURCE_PERMISSIONS )

This ensures that the DLLs are where they need to be once CMake is done generating the projects and there is no need for a post-build step in Visual Studio (even though that would work too).

My applications usually define an EXE_DIR variable which is used to set the RUNTIME_OUTPUT_DIRECTORY_<BUILD_TYPE> target property to set the output directory in Visual Studio, but in case someone doesn't provide said variable the code defaults to CMAKE_CURRENT_BINARY_DIR, i.e. where the project files were generated, which is the default output directory behavior of Visual Studio. If the ExternalSharedLibraries list is empty, nothing gets copied; if the files at the destination already exist only newer files are copied over.

Hth,
--
Marek Vojtko
mail: marek.vojtko at firaxis.com



More information about the CMake mailing list