-1

I have 2 static libs: liba.a, libb.a and a executable that link both. liba.a is calling to function foo which is defined in libb.a both libs compile successfully BUT my exe seems to have linker errors:

undefined reference to `foo'

need help...

using ubuntu 14.04. gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

yehudahs
  • 2,488
  • 8
  • 34
  • 54

1 Answers1

4

When you build:

g++ liba.a libb.a myCode.o -o myExe

This is wrong, because the order of the arguments matters. If myCode uses symbols from liba and libb, those libraries must be specified after it on the command line:

g++ myCode.o liba.a libb.a -o myExe

Alternatively, you can request that the linker treat all three as a "group"; if you do so, dependencies will be resolved for you within that group without needing to worry about order:

g++ "-Wl,--start-group" liba.a libb.a myCode.o "-Wl,--end-group" -o myExe
Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • you are right !!! I always thought that the linker searches ALL the libs and .o file for function names. Didn't know that it searches only forwards... :( it's a pity that I had to spend some hours of my life to understand it. – yehudahs Jan 29 '15 at 17:30
  • 1
    @yehudahs: No, it's a pity you didn't read [the FAQ](http://stackoverflow.com/a/24675715/560648)! – Lightness Races in Orbit Jan 29 '15 at 17:32