2

I installed polarssl:

  1. make
  2. sudo make install

tried to compile very simple file, named test.c:

#include <stdio.h>
#include "polarssl/md5.h"

int main(int argc, char * argv[])
{
  int i;
  for (i=1;i<1;i++)
  {
    char res[16];
    if (md5_file("file.txt",res) == 0)
    {
      int count;
      for (count=0;count<16;count++)
        printf("%02x",res[count]);
      printf("n");
    }
  }
  return 0;
}

Compiled it like this:

gcc -lpolarssl test.c -I /usr/local/include/polarssl/

but it shows me:

/tmp/cczptlsk.o: In function `main':
test.c:(.text+0x36): undefined reference to `md5_file'
collect2: ld returned 1 exit status

whats the problem, how to fix it? I know for 100% that polarssl files are in /usr/local/include/polarssl/

yak
  • 3,770
  • 19
  • 60
  • 111

2 Answers2

4

The compiler will attempt to complete linkage in the order the objects or files are presented. In this case, since you had put -lpolarssl first, there were no unresolved symbols needed from that library, so nothing got linked in.

Putting -lpolarssl last lets the compiler resolve unresolved symbols from your source file from that library.

jxh
  • 69,070
  • 8
  • 110
  • 193
  • thank you so much, wonderful explanation, now I can fully understand it, cheers:) – yak Jun 27 '13 at 17:28
2

Includes are fine.

But linking is wrong. Try to put the -lpolarssl last in the linker command.

Then add a -L if libpolarssl.a is not found by the linker to point it to the right location.

Yohan Danvin
  • 885
  • 6
  • 13
  • So should it be just `gcc test.c -lpolarssl` or `gcc test.c -L /usr/local/include/polarssl/ -lpolarssl `? this works too – yak Jun 27 '13 at 17:16
  • 2
    You can add `-I /usr/local/include/polarssl/` but only if you want to be able to write `#include ` instead of `#include `, which is probably not necessary. Careful, `-L` is for specifying additional locations for compiled libs, as opposed to `-I` which is for addition locations for headers (#includes). – Yohan Danvin Jun 27 '13 at 17:19