CMake `INSTALL` for targets and its SO dependencies
Andrew Mclaughlin
My target linked with several libraries using TARGET_LINK_LIBRARIES with PUBLIC keyword, The INSTALL command looks like INSTALL(TARGETS foo DESTINATION ${CMAKE_INSTALL_PREFIX}/bin). I want somehow to force the cmake to include all (preferably excluding system libraries) libraries (SOs only) I link with to be included in the installation process. I've tried EXPORT keyword but looks like it affects only libraries which I build with in my project and marked with the same EXPORT as foo library.
Is it possible?
EDIT001: Additional information that may affect answer.
I'm using vcpkg to manage third parties. So the TARGET_LINK_LIBRARIES looks like
TARGET_LINK_LIBRARIES(foo PUBLIC GTest::GTest GTest::Main ${GOOGLE_MOCK} event ${THRIFT_LIBRARIES} ${Boost_LIBRARIES} lzo2 sqlite3 ${ZeroMQ_LIBRARY} gRPC::grpc gRPC::grpc++ xml2 stdc++fs bfd -l:libisal.so.2 sgutils2 pthread uuid rt )So, essentially what I want to achieve is to take all these libraries which are macro'ed by vcpkg, like ${THRIFT_LIBRARIES}, ${Boost_LIBRARIES} and gRPC::grpc and so on
2 Answers
As of cmake 3.21, you can now do with:
install(IMPORTED_RUNTIME_ARTIFACTS gRPC::grpc)
install(IMPORTED_RUNTIME_ARTIFACTS ${Boost_LIBRARIES})etc.
See new Install command.
3CMake itself does not allow to install dependencies automatically. This would be a rather hard task, because it would have to consider a lot of corner cases.
Just think of transitive dependencies (I don't know if this is the right word), like: Your libA depends on libB, which depends on libC. How should CMake get this from the CMakeLists, where only libB is listed?
Or: What do you consider a system library? Everything that is not in PATH? How do you know which libraries are installed system-wide on the client's machine?
You see, there are some really tricky things to consider.
Here are some possibilities you have:
- Ask your users to install the dependencies.
- Statically link libraries into your binary.
- Copy library files using
install(FILES files... DESTINATION <dir>). Maybe your dependency manager can help creating the list of files. - Write a script that does something like
windeployqtfor Qt-based applications on Windows: Analyze the binary file (e.g. usingldd myApp) and automatically copy over the required dependencies.