I work on a C++ project where I create a static library (let's call it target1).
This library depends on 2 other libraries (let's call them dep1 and dep2).
The project uses cmake to build target1.
Once it is built, if I run nm -C libtarget1.a | c++filt
and grep for the symbols in dep1 or dep2, they are present but all undefined.
Consequently, users of libtarget1.a gets undefined references at link time. Not useful at all; we want target1 to be complete and independent (wrt dep1 and dep2).
So my question is: How do I tell cmake to add all the symbols from dep1 and dep2 into target1?
Here is what I tried and do not work:
- This answer Combining several static libraries into one using CMake did not make it work: The symbols from dep1 and dep2 were still all undefined.
target_link_libraries()
with various combinations of-Wl,--whole-archive
and-Wl,--no-whole-archive
.target_link_libraries()
with various combinations ofPUBLIC, LINK_PUBLIC, INTERFACE, LINK_INTERFACE_LIBRARIES
.- Using
add_custom_target()
.
Here is something that works (outside cmake though) with flaws:
ar -M <<EOM
CREATE libtarget1all.a
ADDLIB libtarget1.a
ADDLIB libdep1.a
ADDLIB libdep2.a
SAVE
END
EOM
The flaw is that libtarget1all.a contains the symbols from dep1 and dep2 indeed but both as defined and undefined at the same time...