How to cpack dependent shared libraries along with my executable?

I have a cmake project which builds an executable. The executable depends on a 3rd party .so file to run:


add_executable(myapp …)
target_link_libraries(myapp ${3rd_party_so})
#install rules:
set(CMAKE_INSTALL_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/install_root)
install(TARGETS myapp RUNTIME DESTINATION bin)

#Creating distribution package:
include(InstallRequiredSystemLibraries)
include(CPack)

After “cpack -G TXZ”, I only find “myapp” in the bin subfolder of the generated package. Neither system .so files nor the 3rd party .so file are included in the package. What else should I do to make cpack include those .so files?

The BundleUtilities module can help with this. A very basic example would be

set(APPS ...)  # paths to executables
set(DIRS ...)   # directories to search for prerequisites
install(CODE
"
include(BundleUtilities)
fixup_bundle(\"${APPS}\"   \"\"   \"${DIRS}\")
"
 )

There is also file(GET_RUNTIME_DEPENDENCIES) available in 3.15 and newer. This does a more…exact calculation than BundleUtilities (but is restricted to the 3 main platforms of today). Once the list of dependent libraries is found, they can be installed and modified as necessary (I’d like to work on getting CMake to ship some common utilities for doing so, but don’t have a timeline).