0

I have two libraries a and b. The last depends on functions defined in the other (a).

When I link a to b it has no effect.

MWE on wsl2 with g++9 and cmake 3.16:

[...]

add_library(a ${a_SRC})
target_link_libraries(a CONAN_PKG::<foo>)

add_library(b ${b_SRC})
target_link_libraries(b a CONAN_PKG::<bar>)

add_executable(main ${main_SRC})
target_link_libraries(main a b)

I get an undefined reference to error. When I check the size of libb.a it is the same no matter if I link to liba.a or not in target_link_libraries(b a CONAN_PKG::<bar>).

There is something I do not understand here!

I used an ugly workaround, where I include the source files of a in b:

[...]

add_library(a ${a_SRC})
target_link_libraries(a CONAN_PKG::<foo>)

add_library(b ${b_SRC} ${a_SRC})
target_link_libraries(b CONAN_PKG::<bar>)

add_executable(main ${main_SRC})
target_link_libraries(main a b)

  • Please provide the **specific** and complete error message. If the error is appearing during linking of `main`, it could simply be that your link order is incorrect (see [here](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc)). You may need `target_link_libraries(main b a)` instead. – Kevin Aug 07 '20 at 17:46
  • I tried both and it did not change the outcome! – Oussama Ennafii Aug 10 '20 at 08:51
  • The error message is `undefined reference to ` whith being defined in `a.cpp`. – Oussama Ennafii Aug 10 '20 at 08:52
  • Thanks, but this is *still* not the **complete** error message, as it contains redacted information. Please copy/paste the *full* error message (as seen in this question [post](https://stackoverflow.com/q/16574113/3987854)) with surrounding build logs to provide *context*. Without it, it is still unclear where things are going wrong. – Kevin Aug 10 '20 at 12:56

2 Answers2

0

g++ (compared to MSVC) requires the correct order of linked libraries. If your b lib depends on a, than you should put it before a on the link list:

target_link_libraries(main b a)
Dmitry Sokolov
  • 3,118
  • 1
  • 30
  • 35
0

Actually, I misunderstood the issue I had.

In fact, b is a header-only library that depends on a.

The proper formulation is provided here:

Linking a library to a header-only one

Thanks to squareskittles, for his comments.