[CMake] Non OBJECT libraries and population of $<TARGET_OBJECTS:..>

David Hunter davidhunter22 at gmail.com
Mon Jul 10 13:41:13 EDT 2017


Currently you can create an OBJECT library using "add_library(<name>
OBJECT <src>...)" this populates $<TARGET_OBJECTS:name>  which can then
later be used using something like
"target_sources(name PUBLIC $<TARGET_OBJECTS:name>)". I am wondering if
there  is some reason that $<TARGET_OBJECTS:name> can't be populated when
you create a shared or static library, for instance using
"add_library(<name> SHARED <src>...)". Looking at the output the VS 2017
generators for "add_library(<name> OBJECT <src>...)" it seems to actually
build a static library anyway. I suspect that all the <src> files are
compiled to object files somewhere for both STATIC and SHARED libraries,
if so would it not be possible to point $<TARGET_OBJECTS:name> as these
object files?

The reason I ask is that we have a a bunch of source code that builds
in about 100 projects. These projects have a normal hierarchical
dependency tree. However when distributing we want to create larger
libraries that are the combination of various subsets of the smaller ones.
In automake these are called convenience libraries and I suspect they may
have been the reason for CMake OBJECT in the first place. Given the current
behaviour we have to build all the projects twice once in SHARED library
form on once in OBJECT library form. If TARGET_OBJECTS was populated for
SHARED libraries we would not need to build everything twice as we could
build everything SHARED but still use TARGET_OBJECTS to build combination
convenience libraries.

Of course maybe there's already a way to do this without having to
build twice which I don't know about.


More information about the CMake mailing list