8

I'm trying to compile a c-program with openssl-references. I'm using Linux Mint 17.1 and the development package "libssl-dev" is installed.

#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/ssl.h>
...

void send_smtp_request(BIO *bio, const char *req)
{
    BIO_puts(bio, req);
    BIO_flush(bio);
    printf("%s", req);     
}

If I compile the code with:

gcc -o client bio-ssl-smtpcli2.c

I get the this error:

/tmp/ccCHrti2.o: In function 'send_smtp_request':
bio-ssl-smtpcli2.c:(.text+0x1f):  undefined reference to 'BIO_puts'
bio-ssl-smtpcli2.c:(.text+0x3a):  undefined reference to 'BIO_ctrl'

Does someone have an idea how to fix this?

Agricola
  • 572
  • 1
  • 8
  • 20
S1J0
  • 179
  • 1
  • 6
  • 13
  • 1
    Possible duplicate of [Linking problem of OpenSSL library in existing C project](http://stackoverflow.com/questions/5700617/linking-problem-of-openssl-library-in-existing-c-project), [Linking OpenSSLs libcrypto in GCC](http://stackoverflow.com/q/18835219), [Errors that refer to a bunch of unresolved OpenSSL symbols that clearly exist?](http://stackoverflow.com/q/15318978), ... – jww May 09 '15 at 22:13

2 Answers2

17

I managed to compile your function by using :

gcc main.c -o main -I /usr/local/ssl/include -L /usr/local/ssl/lib -lssl -lcrypto -Wall

More explainations :

  • -I /usr/local/ssl/include adds /usr/local/ssl/include to the include search path.

  • -L /usr/local/ssl/lib adds /usr/local/ssl/lib to the library search path.

  • -lssl -lcrypto links libraries libcrypto and libssl

  • -lcrypto must follow -lssl becuase ld is a single pass linker

  • Wall enables all warnings.

My guess is that you are missing -lcrypto.

jww
  • 97,681
  • 90
  • 411
  • 885
francis
  • 9,525
  • 2
  • 25
  • 41
  • I got a new error: /usr/local/ssl/lib//libssl.a(ssl_lib.o): In function `SSL_clear': ssl_lib.c:(.text+0x19e): undefined reference to `COMP_CTX_free' – S1J0 May 11 '15 at 11:44
  • I found [this answer to the same problem](http://ubuntuforums.org/archive/index.php/t-985136.html) : try to put `-lssl` in front of `-lcrypto`. I changed my answer accordingly. – francis May 11 '15 at 11:51
  • Ok no errors anymore :) To complete the answer, I had to add -ldl – S1J0 May 11 '15 at 12:14
0

put -lssl in front of -lcrypto the solved my issue, thanks!

Sunding Wei
  • 1,803
  • 17
  • 12