c++cmakecpack

Packaging exe with a dll not linked in CMake


I'm still inexperienced with CMake/CPack so hopefully I can make this make sense.

In my CMake project I'm loading a .dll file using LoadLibrary in the C++. I have no control over the .dll and it doesn't have any accompanying .lib files so as I understand I can't use CMake to do the loading/linking. It does have a header that defines the API, whose functions I'm loading manually.

I'm doing the packaging using the following:

# install
include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    RUNTIME_DEPENDENCIES
        PRE_EXCLUDE_REGEXES
            "api-ms-.*" # windows API
            "ext-ms-.*" # windows API
            "^hvsifiletrust\\.dll$"
            "^pdmutilities\\.dll$"
            "^vc.*"
            "^msvcp.*"
            "^concrt.*"
        POST_EXCLUDE_REGEXES
            ".*WINDOWS[\\/]system32.*" # windows system dlls
        DIRECTORIES 
            "${CMAKE_SOURCE_DIR}/external/"
            "${ADDITIONAL_PACKAGES_ROOT}/"
    RUNTIME DESTINATION "${CMAKE_INSTALL_CONFIG_NAME}/"
)


# --- microsoft DLLs and others
set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)
set( CMAKE_INSTALL_DEBUG_LIBRARIES TRUE )
set( CMAKE_INSTALL_UCRT_LIBRARIES  TRUE )
include(InstallRequiredSystemLibraries)
install(PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS}
        DESTINATION "${CMAKE_INSTALL_CONFIG_NAME}/bin"
        COMPONENT applications)

# packaging        
set(CPACK_PACKAGE_NAME "CDL")
set(CPACK_GENERATOR "ZIP")
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF)
set(CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE ON)
include(CPack)

This pulls most of the correct dependencies into the zip, as far as I can tell, but as expected it's not getting the .dll that I'm loading in the code.

What do I add to the install (or elsewhere) that can grab the extra .dll and it's dependencies and package them with the rest?


Solution

  • In this case, the providers of the dll had all their dependencies in the same place as the dll we're using, so I was able to use install(DIRECTORY...) to solve my problem.