0

I am importing three static libraries (A.a, B.a, C.a) with C depends on A and B, B depends on A. In my program I use functions in C.

(Ubuntu 16.04 and CMake 3.5.1) In my cmake file I use target_link_libraries() to link A.a, B.a, C.a with ordered to my executable file. However when compiling it shows "undefined reference to funcA()", where funA is Undefined Symbol in C.a (showed with nm command), this function is actually defined in A.a as a Text Symbol.

target_link_libraries(myExec A.a B.a C.a)

I expect to see linking static libraries correctly without those undefined symbol.

Update As Mike mentioned in the comment:

a library must be appear in the linkage sequence before those that it depends on

Therefore,

target_link_libraries(myExec C.a B.a A.a)

is the correct order

milanow
  • 159
  • 1
  • 13
  • This is a helpful page for understanding cmake:https://github.com/pr0g/cmake-examples#modern-cmake-examples – tswanson-cs Apr 17 '19 at 18:42
  • Thanks @ThomasSwanson I would check that first – milanow Apr 17 '19 at 18:44
  • Yea @Amadeus many answers I found there are resolving static libraries dependencies with gcc. Not with CMake. At least I didn't find out solution here, I have searched over the forum and tried the methods for hours. But none works for me. – milanow Apr 17 '19 at 19:00
  • @Amadeus However my static library is from an external build. I don't want to include it as a submodule in my project. – milanow Apr 17 '19 at 19:18
  • On Ubuntu 16.04 your toolchain is GCC, or possibly clang. The system linker's static library linkage rules apply: a library must be appear in the linkage sequence before those that it depends on. `A.a B.a C.a` is the wrong order. `C.a B.a A.a` is the right order. – Mike Kinghan Apr 17 '19 at 19:25
  • @MikeKinghan Thanks! You are correct. – milanow Apr 17 '19 at 19:44

0 Answers0