-1

I am trying to understand the cause of a linking error. This example is specific, but the cause is probably a general principle I don't understand.

I wanted to build the minimal example from this post. Ubuntu 14.05.5. I did:

$ apt-get install libssl-dev
$ opensll version
OpenSSL 1.0.1f 6 Jan 2014
$ echo $LD_LIBRARY_PATH
/usr/local:
$ gcc  -I/usr/include/openssl -o test md5.c  -L/usr/lib/x86_64-linux-gnu -lssl
/tmp/ccUSgfgs.o: In function `main':
md5.c:(.text+0x26): undefined reference to `MD5_Init'
md5.c:(.text+0x68): undefined reference to `MD5_Update'
md5.c:(.text+0xab): undefined reference to `MD5_Final'
collect2: error: ld returned 1 exit status

Which throws a common linking error. It can't find the definitions to those symbols. Through random attempts, I found this builds:

$ gcc  -I/usr/include/openssl -o test md5.c  -L/usr/lib/x86_64-linux-gnu -lcrypto    

The only difference is '-l crypto' instead of '-l ssl'

Why does one build and the other not?

Community
  • 1
  • 1
Tyson Hilmer
  • 543
  • 5
  • 20
  • Also see [How to create a md5 hash of a string in C?](http://stackoverflow.com/q/7627723) – jww Jan 17 '17 at 23:30

1 Answers1

0

Because libcrypto.a contains the definitions, not libssl.a. The proof is:

$ nm /usr/lib/x86_64-linux-gnu/libssl.a | grep MD5
             U MD5_Init
             U MD5_Transform

Well, one of the symbols is there, but U means undefined. Rather:

$ nm /usr/lib/x86_64-linux-gnu/libcrypto.a | grep MD5
  nm: ebcdic.o: no symbols
  00000000000001f0 T MD5_Final
  0000000000000340 T MD5_Init
  00000000000001e0 T MD5_Transform
  0000000000000000 T MD5_Update

Symbols are there with definitions. Case solved. Probably could have saved myself some time by reading the documentation.

Tyson Hilmer
  • 543
  • 5
  • 20