0

I use a static library, let's assume cityhash, which I've build and installed to /usr/local/lib. I have a file foo.cxx which uses cityhash, for example:

// foo.cxx
u64 get_hash(const std::string &s) {
  return CityHash64(s.data(), s.size());
}

I build a static library from it:

gcc -c foo.cxx  => foo.o
ar rcs libfoo.a foo.a  => libfoo.a

I have another file, bar.cxx, which uses foo.cxx and indirectly CityHash function. I compile it, and link with both libcityhash.a and libfoo.a like the following:

gcc -c bar.cxx  => bar.o
gcc -L. -o bar bar.o -lcityhash -lfoo

But this doesn't work, linker complains that CityHash64 is undefined reference. What is wrong? When I do not make a static library libfoo.a everything works fine.

demi
  • 5,384
  • 6
  • 37
  • 57

1 Answers1

2

See this. You need to write linker args -lfoo -lcityhash. Library that needs symbol should go before the one that provides it.

Why does the order in which libraries are linked sometimes cause errors in GCC?

Community
  • 1
  • 1
grifcj
  • 383
  • 6
  • 15