-1

When I include "stdio.h" and try to use "printf" it works fine. In the same way when I use "krb5.h", its function "krb5_get_init_creds_password" does not get resolved by its own.

Error faced : bash-4.1$ gcc krb.c krb.c: In function ‘main’: krb.c:6: warning: incompatible implicit declaration of built-in function ‘printf’ /tmp/ccTK4DJM.o: In function main': krb.c:(.text+0x53): undefined reference tokrb5_get_init_creds_password' collect2: ld returned 1 exit status

I had to compile using -lkrb5 which resolved the issue.

Can someone let me know why do I need to use gcc option "-lkrb5" to get that krb5 function resolved ? I am using C.

Mayank
  • 2,150
  • 1
  • 15
  • 13

1 Answers1

2

The header file you include only contains the declaration of the function, the actual function definition (the implementation) is in the actual library (usually a file named (using your example) libkrb5.a or libkrb5.so).

The option you pass to the linker is -l (lower-case L) which tells the linker to find and use whatever library you pass as argument to the -l option.


If you have create programs consisting of multiple source files yourself, then this should not have been much of a surprise really. If you have two source files a.c and b.c, where in a.c you call a function defined in b.c you need to declare the function for it to be available in a.c. And then when you link the executable program you need to link with the object file created by b.c to be able to find the definition.

Using a library is really not much different from that.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621