IMPORTED_RUNTIME_ARTIFACTS with and without RUNTIME_DEPENDENCY_SET

myLib:

add_library(myLib SHARED lib.cpp lib.h)
target_include_directories(myLib PUBLIC ${CMAKE_CURRENT_LIST_DIR})

main:

add_executable(main main.cpp)
target_link_libraries(main PRIVATE myLib)

install(TARGETS main
    RUNTIME DESTINATION out
)

install(IMPORTED_RUNTIME_ARTIFACTS main
    RUNTIME_DEPENDENCY_SET mainDeps
)

install(RUNTIME_DEPENDENCY_SET mainDeps
    LIBRARY DESTINATION out
)

Result (looks good to me):

...
[build] -- Installing: .../out/main
[build] -- Installing: .../out/myLib.so
...

However:

main:

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
...
  1. Shouldn’t these behave the same way, though?
  2. I suspect everything is wrong from the start because myLib isn’t an IMPORTED target. Is that correct?
  3. 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)?
  1. Shouldn’t these behave the same way, though?

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.

  1. 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.

  1. 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.

1 Like
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.

Oh, right, my bad.

Is myLib IMPORTED at this point? If so, I would expect install(TARGETS) to give the error that it gave (yes, I know that error is confusing.)

What I mean is, are main and myLib built as part of the same project or different projects?

main and myLib are part of the same project:

add_subdirectory(myLib)
add_subdirectory(main)

where those directories each contain a CMakeLists.txt with the corresponding contents I gave in the OP.

I’ve tried with cmake 3.21.1 and 3.23.3, if that matters.

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.

1 Like

That was it! It works now. Thank you!!