add_executable(main main.cpp)
target_link_libraries(main PRIVATE myLib)
install(TARGETS main
RUNTIME DESTINATION out
)
install(IMPORTED_RUNTIME_ARTIFACTS main
LIBRARY DESTINATION out
)
Result (myLib.so not installed):
...
[build] -- Installing: .../out/main
...
Shouldn’t these behave the same way, though?
I suspect everything is wrong from the start because myLib isn’t an IMPORTED target. Is that correct?
What’s the canonical way to achieve what I’m going for, which is to install an executable (or shared library) and its runtime library dependencies all in the same directory? I understand that there might be nuance between imported libraries (like Boost) and libraries that I define myself (like myLib). Do I need to manually identify and install the dependencies myself (e.g., install(IMPORTED_RUNTIME_ARTIFACTS ${Boost_LIBRARIES})? Or is there a more “automatic” way of doing this (something that my first main CMakeLists.txt seems to imply)?
No. IMPORTED_RUNTIME_ARTIFACTS only installs the artifacts from that specific target, not from any of its dependencies. RUNTIME_DEPENDENCY_SET is needed to install the dependencies.
I suspect everything is wrong from the start because myLib isn’t an IMPORTED target. Is that correct?
Neither main nor myLib is IMPORTED, so yes, IMPORTED_RUNTIME_ARTIFACTS is not what you want here.
What’s the canonical way to achieve what I’m going for, which is to install an executable (or shared library) and its runtime library dependencies all in the same directory? I understand that there might be nuance between imported libraries (like Boost) and libraries that I define myself (like myLib).
install(TARGETS main myLib
RUNTIME DESTINATION out
LIBRARY DESTINATION out
RUNTIME_DEPENDENCIES)
Since myLib is not IMPORTED, it has to be installed manually with install(TARGETS). However, Boost et al can be captured with RUNTIME_DEPENDENCIES since they’re IMPORTED.
install(TARGETS main myLib
RUNTIME DESTINATION out
LIBRARY DESTINATION out
RUNTIME_DEPENDENCIES)
Since myLib is not IMPORTED, it has to be installed manually with install(TARGETS). However, Boost et al can be captured with RUNTIME_DEPENDENCIES since they’re IMPORTED.
I tried this, but cmake gave an error:
[cmake] install TARGETS given unknown argument "RUNTIME_DEPENDENCIES".
Looking at the docs, it seems that RUNTIME_DEPENDENCIES should maybe come first:
install(TARGETS main myLib
RUNTIME_DEPENDENCIES
RUNTIME DESTINATION out
LIBRARY DESTINATION out
)
But then cmake complains:
[cmake] install TARGETS given target "myLib" which does not exist.
Confusing since it obviously exists, I can link against it.
The fact that myLib is in a different subdirectory from where the install() is happening might not be helping. Try moving the install() to the top directory after the add_subdirectory() calls.