How to use OpenSSL with GCC?

Viewed 69665

I'm trying to use openssl in a gcc program but it isn't working.

g++ server.cpp /usr/lib/libssl.a -o server

gives an error message, as does anything with the -l option. What must I type on the command line to link with openssl? The file /usr/lib/libssl.a exists, but nevertheless I still get the linker error no such function MD5() exists.

5 Answers

Without knowing the exact errors you are seeing, it is difficult to provide an exact solution. Here is my best attempt.

From the information you provided, it sounds as though the linker is failing because it cannot find a reference to the md5 function in libssl.a. I believe this function is actually in libcrypto so you may need to specify this library as well.

g++ server.cpp -L/usr/lib -lssl -lcrypto -o server

The location of the library is not fixed. In my case (Ubuntu 18.04), the .a files are located in /usr/lib/x86_64-linux-gnu/. So here are the complete steps:

1) install the library,

sudo apt install libss-dev

2) check the installed files,

dpkg-query -L libssl-dev

3) change the gcc flags -L(library directory) -l(library name), e.g.,

gcc XXX.c XXXXX.c -L/usr/lib/x86_64-linux-gnu/ -lcrypto -lssl
Related