-1

I am trying to link two static libraries using cmake but I don't get the result I expect when I look into the libraries using ar -t.

A small example where library A is linked to B:

File a.c:

void a(){}

File b.c:

void b(){}

File CMakeLists.txt:

project(test C)

add_library(a STATIC "a.c")
add_library(b STATIC "b.c")
target_link_libraries(a b)

I expected to see both object files in the output ar -t liba.a but instead I only see a.c.o in the output.

Matt
  • 63
  • 1
  • 8
  • Have you checked CMake documentation before, to verify your expectations? – Sergei Nikulov Oct 04 '18 at 07:52
  • 1
    Possible duplicate of [Link static library using CMake](https://stackoverflow.com/questions/18901128/link-static-library-using-cmake) – Tsyvarev Oct 04 '18 at 07:55
  • 1
    When a **static** library is created, linking stage isn't actually performed. This is not a specific of CMake, this just an origin of static libraries. In your case call to `target_link_libraries(a b)` affects only on usage of `a` library later in the project. E.g. if an executable will link to `a` library, it will be automatically linked to `b` library. But again, the file, corresponded to `a` library, doesn't contain information about `b` library. – Tsyvarev Oct 04 '18 at 07:59
  • @Tsyvarev I can see now my question is not new and you are right, but do you have a link to the documentation about your assertion? – Matt Oct 04 '18 at 09:50
  • No, I don't have a link to documentation. But there is a lot of question on Stack Overflow about this. E.g.: https://stackoverflow.com/questions/2157629/linking-static-libraries-to-other-static-libraries or https://stackoverflow.com/questions/9334257/how-do-i-statically-link-a-library-into-another-static-library. – Tsyvarev Oct 04 '18 at 10:14
  • Possible duplicate of [Combining several static libraries into one using CMake](https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake) – YSC Jan 24 '19 at 10:12

1 Answers1

0

You can always debug your CMakeLists.txt using the VERBOSE=ON argument to make (on Linux) this way you can verify if the commands are the one you expect.

$> make VERBOSE=ON

In your case CMake scan the dependencies of a.c and it discard b.c as real dependency for a.c, that's why it's not linked.

Luis Ehlen
  • 11
  • 4