2

When I compiled my program and ran it, I got a a symbol lookup error. I was doing this:

$ gcc -o parts parts.c -lnettle
$ ./parts
$ ./parts: symbol lookup error: ./parts: undefined symbol: nettle_pbkdf2

My code included these header files:

#include <nettle/pbkdf2.h>
#include <nettle/hmac.h>
#include <pbkdf2-hmac-sha1.c>

I solved my problem by including the object files for the two included header files during gcc compilation.

$ gcc -o parts parts.c hmac.o pbkdf2.o -lnettle

The thing is, I don't understand what is going on and therefore why this works. Why must I include the .o files and not just the header files to avoid symbol lookup or undefined reference errors?

userYou
  • 65
  • 1
  • 7

1 Answers1

2

As Tobias mentioned, a header file tells the compiler what is done, the object file tells the compiler how it is done. You can see here what an object file is, but in reality it's just a precompiled version of a source file.

Truly, you were not actually getting compiler errors, but linker errors. It knew how to compile your source file, but it couldn't put everything together until it got the other object files.

Community
  • 1
  • 1