23

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Jondo Zaro
  • 231
  • 1
  • 2
  • 3
  • 1
    I don't see the use of -l in te command you posted. And don't post your question in UPPER CASE - shouting is rude. –  Dec 12 '09 at 17:01
  • 2
    Something like `g++ server.cpp -L/usr/lib -lssl -o server` should do the trick. Post the exact errors that you are getting and someone can help. And I second Neil on the shouting thing ;) – D.Shawley Dec 12 '09 at 17:03
  • 1
    You're missing `/usr/lib/libcrypto.a`. `libcrypto` provides the crypto used by `libssl`. Add both of these in this exact order because LD is a single pass liker: `/usr/lib/libssl.a /usr/lib/libcrypto.a`. – jww Sep 17 '15 at 23:52

5 Answers5

28

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

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
jschmier
  • 15,458
  • 6
  • 54
  • 72
11

You or others may find this article developerWorks article helpful.

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

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

will
  • 4,799
  • 8
  • 54
  • 90
6

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

user3343214
  • 61
  • 1
  • 1
6

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
Alt Eisen
  • 598
  • 6
  • 9
  • still does not work for me. and `libcrypto.{so/a}` exists where `-L` is pointing at – Nimitz14 Apr 04 '19 at 21:56
  • Any error message? Is it still from libcrypto? e.g., If you are using c++, then do not copy & paste my command, use c++ instead. – Alt Eisen Apr 05 '19 at 04:33
  • 1
    nevermind I was using an example program from the openssl website but had not copied all the code... lol – Nimitz14 Apr 05 '19 at 13:51
  • 1
    in the first step should the package be `libssl-dev`? – Dipu Jul 29 '19 at 04:44
  • @Dipu Yes, it should be. Unfortunately, Stackoverflow doesn't let less than one char edits – Bat Jul 29 '21 at 06:44
  • The thing that made it work for me, is putting the file to act upon at the front of the gcc statement. So `gcc ./test.c -L/usr/lib -lssl -lcrypto` works, but `gcc -L/usr/lib -lssl -lcrypto ./test.c` does not – lightstack Oct 27 '21 at 18:36
0

On top of the accepted answers, I could not make compile the OpenSSL example for AES-CCM:

https://github.com/openssl/openssl/blob/master/demos/evp/aesccm.c

To make it work I needed to add two more things:

  • The Dinamic Linking Library : -ldl
  • The PThread library to use POSIX threading support: -pthread (Adding directly the library with -lpthread is not recommended )
Community
  • 1
  • 1
renzoe
  • 145
  • 1
  • 3
  • 9