0

What is the defacto way of handling implicit linking in CMake projects?

In windows, .dll's are linked during runtime, thus CMake only needs to link the .lib files during compilation. This is better done using find_package. However, during compilation, CMake never copies over the .dll corresponding to the linked .lib file to the linked executable's output path.

Is this something that CMake leaves up to the user?

It seems kind of messy, not to mention non-cross platform, to have to manually find dll's for each library you link and either manually copy them over to your output directory or write individual CMake commands, as shown below, to do this per package you find/library you link.

add_custom_command(
        TARGET MyTarget POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "${PROJECT_SOURCE_DIR}/libs/SDL2.dll"
        $<TARGET_FILE_DIR:MyTarget>
    )
Francisco Aguilera
  • 3,099
  • 6
  • 31
  • 57
  • 1
    [They suggest](http://cmake.3232098.n2.nabble.com/Copying-of-3rd-party-DLLs-in-a-POST-BUILD-step-tp7167258p7168360.html) to use CMake modules [BundleUtilities](https://cmake.org/cmake/help/v3.7/module/BundleUtilities.html) or [GetPrerequisites](https://cmake.org/cmake/help/v3.7/module/GetPrerequisites.html) – Tsyvarev Feb 09 '17 at 07:36
  • 1
    Possible duplicate of [How to copy DLL files into the same folder as the executable using CMake?](http://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) or [CMake post-build-event: copy compiled libraries](https://stackoverflow.com/questions/40696990/cmake-post-build-event-copy-compiled-libraries) – Florian Feb 09 '17 at 08:10
  • 1
    But I see this more as a limitation of the build tool/operating system. So why is Visual Studio not able to copy its C++ DLL dependencies like it's doing for C# dependencies? See also [How to make Visual Studio copy a DLL file to the output directory?](http://stackoverflow.com/questions/1776060/how-to-make-visual-studio-copy-a-dll-file-to-the-output-directory), VS also uses post-build actions. I also found [the following approach](http://stackoverflow.com/questions/1005901/how-to-set-path-environment-variable-using-cmake-and-visual-studio-to-run-test) interesting. – Florian Feb 09 '17 at 08:23
  • @Florian VS does have a function for post-build actions, however, this needs to be handled at the CMake level because CMake needs to be able to generate such projects for many different IDEs not just VS. – Francisco Aguilera Feb 09 '17 at 21:24

0 Answers0