Unable to use WHOLE_ARCHIVE on two static library that depends on each other
Please see below minimal example:
add_library(Foo STATIC foo.cpp foo.h)
add_library(Bar STATIC bar.cpp bar.h)
target_link_libraries(Bar Foo)
add_library(Baz SHARED baz.cpp baz.h)
target_link_libraries(Baz $<LINK_LIBRARY:WHOLE_ARCHIVE,Foo,Bar>)
This produces error
Impossible to link target 'Baz' because the link item 'Foo', specified
without any feature or 'DEFAULT' feature, has already occurred with the
feature 'WHOLE_ARCHIVE', which is not allowed.
However, this looks like it should be a valid situation, so I think it is a CMake bug. If Baz
only links to Bar
, it compiles, but I need the symbols in Foo
to go into Baz
. If I don't let Bar
link to Foo
, CMake works, but it won't compile because Bar
in reality depends on Foo
.