Non-OBJECT library prerequisites are not propagated through OBJECT libraries
I'm using CMake 3.13.4. I saw a number of issues already filed discussing OBJECT
libraries that depend on other OBJECT
libraries, but this is different: here the OBJECT
library is depending on a simple STATIC
library and that is not being propagated as expected.
Here's an example without OBJECT
libraries:
$ cat >CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.13)
project(Test C)
add_library(base STATIC base.c)
target_compile_definitions(base PUBLIC HAVE_BASE)
add_library(inter STATIC inter.c)
target_compile_definitions(inter PUBLIC HAVE_INTER)
target_link_libraries(inter PRIVATE base)
add_executable(exec exec.c)
target_link_libraries(exec PRIVATE inter)
EOF
$ touch base.c inter.c
$ echo 'int main(char** argv, int argc) { return 0; }' > exec.c
This works fine; the final link line is as expected:
/usr/bin/cc CMakeFiles/exec.dir/exec.c.o -o exec libinter.a libbase.a
However, if I change the type of the inter
target from STATIC
to OBJECT
now it no longer works properly:
/usr/bin/cc CMakeFiles/exec.dir/exec.c.o CMakeFiles/inter.dir/inter.c.o -o exec
Note that we do include the inter.c.o
object, BUT the libbase.a
library is missing: it's not inherited from the target_link_libraries()
of the inter
target.
Looking at properties we can see that in the working case with STATIC
the inter
target has this property set:
inter : INTERFACE_LINK_LIBRARIES = $<LINK_ONLY:base>
but when using the OBJECT
library this property is not set at all.
Indeed, I can "fix" the problem by forcibly setting that property by hand; adding:
set_property(TARGET inter APPEND PROPERTY
INTERFACE_LINK_LIBRARIES $<LINK_ONLY:base>)
gives us the right link line:
/usr/bin/cc CMakeFiles/exec.dir/exec.c.o CMakeFiles/inter.dir/inter.c.o -o exec libbase.a