I\'m trying to write a cmake script for installing a project I\'m working on. Part of this is the necessary install(EXPORT LIB_EXPORTS ...)
where LIB_EXPORTS<
we can't install a target that this subdirectory hasn't created
First off, this isn't quite true. Since you're adding your dependencies with add_subdirectory
, they are not IMPORTED
; this means they are globally visible and it doesn't matter where in the project they were created. It just matters that they have been created in time for the call to install(TARGETS)
.
One convenient approach is to put all your packaging commands in packaging/CMakeLists.txt
and then call add_subdirectory(packaging)
at the end of your top-level CMakeLists.txt
so that every target has been created before the first install()
call.
You would then add the targets to your normal export set:
install(TARGETS sc2api sc2renderer c-library SDL2-static
EXPORT LIB_EXPORTS
...)
install(EXPORT LIB_EXPORTS ...)
This is the preferred way of doing things.
Frankly, I'm not sure why including these are necessary, since the libraries should be statically linked into the targets in my project.
They might carry their own usage requirements, like other libraries they depend on. So when exporting, CMake needs to recreate everything that's relevant to using your library, including all transitive dependencies. If you're absolutely certain that this will never be a concern, you can use $<BUILD_INTERFACE:...>
to block the dependency from exporting.
add_library(sc2api SHARED)
target_link_libraries(sc2api PRIVATE $<BUILD_INTERFACE:c-library>)
This is occasionally useful for INTERFACE
libraries that collect warning flags or the like, and would export as empty targets.