[cmake-developers] Object library linking (and a bit about INTERFACE_SOURCES)

Brad King brad.king at kitware.com
Wed Feb 25 10:47:58 EST 2015


On 02/24/2015 05:25 PM, Stephen Kelly wrote:
>> Until that problem is solved we cannot make object libraries
>> implicitly offer their objects just through tll().
> 
> Honestly, the problem is still not clear to me.

Consider an object library with some usage requirements:

 add_library(objlib OBJECT ...)
 target_compile_definitions(objlib PUBLIC MYDEF)

Now try using that library and assume that tll() takes the objects
without explicit $<TARGET_OBJECTS:objlib> in the sources:

 add_library(mylib SHARED ...)
 target_link_libraries(mylib PUBLIC objlib)

We link publicly to forward the usage requirements of objlib through
mylib.  Now consumers of mylib will get objlib's objects too.  That
is not desired.  It is the same problem as transitive interface
sources.

My solution to that when first designing object libraries was to
disallow the tll() altogether and require the consumer that wants
the object files to reference them as $<TARGET_OBJECTS:objlib>
sources explicitly.  Of course that pre-dated usage requirements.
Now we want to allow

 add_library(mylib SHARED ... $<TARGET_OBJECTS:objlib> ...)
 target_link_libraries(mylib PUBLIC objlib)

to put the objects in mylib and also forward usage requirements.
So long as the objlib does not have any INTERFACE_SOURCES then
it will work well.

What I envision for the future is:

 add_library(objlib OBJECT ...)
 target_compile_definitions(objlib PUBLIC MYDEF)
 target_sources(objlib INTERFACE $<TARGET_OBJECTS:objlib>)

That will allow consumers of objlib to get the objects through
tll() just like any other INTERFACE_SOURCES.  What I said in my
previous response was that until the transitive interface sources
problem is solved I'd rather not allow this.  OTOH, since this
population of INTERFACE_SOURCES is not automatic perhaps it is
simplest and most consistent to go ahead and allow projects to
do this.  The problem will be no worse than for other interface
sources.

-Brad



More information about the cmake-developers mailing list