3
votes

I am trying to get OpenSSL to work, but it seems to have a problem with linking. Here is what I did:

  1. I downloaded OpenSSL for Linux from https://www.openssl.org/source/ I tried versions 0.9.8zc, 1.0.0o and 1.0.1j, all with the same result.
  2. I installed each OpenSSL version using ./config, make and sudo make install.
  3. For debugging purposes, I went to /usr/lib/ssl and used sudo chmod -R 777 * to remove any restrictions that could have caused the error.
  4. I created the following program:

main.c:

#include <errno.h>
#include <malloc.h>
#include <resolv.h>
#include <openssl/bio.h>
#include <openssl/ssl.h>
int main(void) {
    SSL_load_error_strings();
    return EXIT_SUCCESS;
}
  1. I created the following makefile in the same directory as my .c file:

makefile:

all: main.o
    cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl
main.o: main.c
    cc -c -Wall main.c -I/usr/local/ssl/include/ -o main.o
  1. When I run the makefile, I get the following error:

cc -o main main.o -L/usr/local/ssl/lib/ -lcrypto -lssl /usr/local/ssl/lib//libssl.a(ssl_err2.o): In function SSL_load_error_strings': ssl_err2.c:(.text+0x4): undefined reference toERR_load_crypto_strings' /usr/local/ssl/lib//libssl.a(ssl_err.o): In function ERR_load_SSL_strings': ssl_err.c:(.text+0xc): undefined reference toERR_func_error_string' ssl_err.c:(.text+0x28): undefined reference to ERR_load_strings' ssl_err.c:(.text+0x3c): undefined reference toERR_load_strings' collect2: ld returned 1 exit status make: *** [all] Error 1

What am I doing wrong?

Cheers Alex

1

1 Answers

8
votes

As answered on the maillist by scott_n but for the record here, swap the order to -lssl -lcrypto.

Explanation: for static C libraries in general on nearly all systems, members of library files like libxxx.a are only pulled in by the linker if they define things referenced from translation units already linked i.e. to the left in the command line. OpenSSL libssl has (numerous) references to libcrypto. If you link -lcrypto first, those references haven't been seen, so the libcrypto files aren't linked; then you link -lssl and create the unsatisfied references. In cases of mutual dependency also called recursive dependency you may need to repeat a library like -lcrypto -lssl -lcrypto but OpenSSL has no such "backward" references.