2

I write a C++ library and when linking against the library the symbols in it cannot be found. Here's what I've got:

a.cpp:

void zak()
{
}

test.cpp:

extern void zak();

int main(int argc, const char ** argv)
{
    zak();
}

Makefile:

all:
    g++ -c -o a.o a.cpp
    ar r libzak.a a.o
    g++ -L. -lzak test.cpp -o test

Here is what make says on my (Linux Mint 13) box:

g++ -c -o a.o a.cpp
ar r libzak.a a.o
g++ -L. -lzak test.cpp -o test
/tmp/ccC4cnLV.o: In function `main':
test.cpp:(.text+0x7): undefined reference to `zak()'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

I am sure I am missing something obvious, but what is it?

Johannes Thoma
  • 1,026
  • 10
  • 21
  • We can kinda work it out from the title of the question, but could you please edit the question itself with the precise error messages? – NPE Feb 06 '14 at 16:24

2 Answers2

5

Link order matters. Put -lzak after test.cpp on the link line.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

I think that -l is for shared libraries (.so). Try this: g++ libzak.a test.cpp -o test

gabriel
  • 235
  • 1
  • 2
  • 9