1

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 of PUBLIC, 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...

Maitre Bart
  • 157
  • 1
  • 11
  • `do I tell cmake to add all the symbols from dep1 and dep2 into target1?` this is unrelated to cmake, this is just how linker works. – KamilCuk Jul 14 '20 at 16:51
  • 1
    Linking static libraries together for obtain a single library which has definition for all functions (and other symbols) is not the task supported by CMake out-of-the-box. Howerer, using a custom command or custom target with proper command line you may achieve that. See [duplicate question](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) for more details. – Tsyvarev Jul 14 '20 at 17:02

0 Answers0