How to use OpenSSL with GCC?

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.


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


You or others may find this article developerWorks article helpful.

  • Secure programming with the OpenSSL API
    • https://developer.ibm.com/technologies/linux/tutorials/l-openssl

It describes most things you need to know to get off the ground with OpenSSL and C/C++. If you find you are following most of the same steps, it might help you see what needs doing.

Good luck.


update

  • revised link: https://developer.ibm.com/technologies/linux/tutorials/l-openssl
    • Which has been shuffled around
  • original link: http://www.ibm.com/developerworks/linux/library/l-openssl.html
    • Which now goes to a digest page including the article.

Note: keeping both links because they be used to find new discoveries.


In Eclipse IDE select Your Project property --> c/c++ Build --> Settings gcc c linker(from tools settings)--> add to Library Search Path (-L)

/usr/lib -lssl -lcrypto


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