Transitive linking for OBJECT libraries does not propagate sources or objects to final target
With the ability to use OBJECT libraries as link targets and linked targets (#14778 (closed)), we are getting very close to making OBJECT libraries real first-class citizens.
However, using OBJECT libraries transitively, through objectlib1
-> objectlib2
-> final_target
doesn't work.
If you will closely look at the second example in the target_link_libraries documentation on OBJECT libraries, you will notice that when you link an object library to an object library, the first object libraries objects are not injected into the second libraries INTERFACE_LINK_LIBRARIES
or added to the SOURCES
property:
add_library(A SHARED a.c)
target_compile_definitions(A PUBLIC A)
add_library(obj OBJECT obj.c)
target_compile_definitions(obj PUBLIC OBJ)
target_link_libraries(obj PUBLIC A)
add_library(obj2 OBJECT obj2.c)
target_link_libraries(obj2 PUBLIC obj)
add_executable(main2 main2.c)
target_link_libraries(main2 obj2)
The documentation claims:
compiles obj2.c with -DA -DOBJ, creates executable main2 with object files from main2.c and obj2.c, and links main2 to A.
Notice the lack of the obj.c
source file in the final compilation. This is not a documentation mistake, this actually bit me when I was merrily porting over my build files to CMake 3.12, the current implementation requires me to manually inject them via target_sources
and generator expressions.
IMO, if we call this collection of object files a "library", we might as well let it behave like an actual library, in particular linking the library code to the target in one way or another. In the end, that is what "linking" means: you link two pieces of code together into one entity (be it a library or an executable).
Is this an oversight, or is that on purpose and I misunderstood the concept of object libs and/or linking?