简体   繁体   中英

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.

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.

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) , eg,

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM