OBJECT libraries are not propagated when linked into INTERFACE libraries
// obj.cpp
int foo() {
return 123;
}
// main.cpp
#include <iostream>
int foo();
int roo() {
return foo();
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.12)
project(wat)
add_library(obj OBJECT obj.cpp)
add_library(int INTERFACE)
target_link_libraries(int INTERFACE obj)
add_library(main SHARED main.cpp)
target_link_libraries(main PUBLIC int)
add_executable(exec exec.cpp)
target_link_libraries(exec main)
On Linux, libmain.so
will link just fine, but won't know where to find foo:
$ readelf -Wa libmain.so | grep -P 'foo|roo'
0000000000004020 0000000300000007 R_X86_64_JUMP_SLOT 0000000000000000 _Z3foov + 0
3: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _Z3foov
9: 0000000000001180 11 FUNC GLOBAL DEFAULT 11 _Z3roov
45: 0000000000001180 11 FUNC GLOBAL DEFAULT 11 _Z3roov
48: 0000000000000000 0 NOTYPE GLOBAL DEFAULT UND _Z3foov
which will cause linking exec
to fail with undefined reference to foo()
On Windows, linking main.dll
will fail with unresolved external symbol foo(void) referenced in function roo
. In both cases, the main
linking command will mention only main.obj
and not obj.obj
.
If I link obj
into main
, instead of int
, everything is OK. Tested with CMake 3.13.1.