C++ 如何在 GCC 中使用 OpenSSL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1894013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:30:07  来源:igfitidea点击:

How to use OpenSSL with GCC?

c++gcclinker

提问by Jondo Zaro

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

我正在尝试在 gcc 程序中使用 openssl 但它不起作用。

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

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

给出一条错误消息,与该-l选项的任何内容一样。我必须在命令行上输入什么才能与 openssl 链接?该文件/usr/lib/libssl.a存在,但我仍然收到链接器错误no such function MD5() exists

回答by jschmier

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 md5function in libssl.a. I believe this function is actually in libcryptoso you may need to specify this library as well.

根据您提供的信息,听起来好像链接器失败了,因为它md5libssl.a. 我相信这个函数实际上是在libcrypto所以你可能需要指定这个库。

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

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

回答by will

You or othersmay find this article developerWorksarticle helpful.

您或其他人可能会发现这篇developerWorks文章很有帮助。

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.

它描述了使用 OpenSSL 和 C/C++ 需要知道的大多数事情。如果您发现您正在遵循大多数相同的步骤,它可能会帮助您了解需要做什么。

Good luck.

祝你好运。



update

更新

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

注意:保留这两个链接是因为它们被用来寻找新的发现。

回答by user3343214

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

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

/usr/lib -lssl -lcrypto

/usr/lib -lssl -lcrypto

回答by Alt Eisen

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:

图书馆的位置不固定。在我的情况下(Ubuntu 18.04),.a 文件位于/usr/lib/x86_64-linux-gnu/. 所以这里是完整的步骤:

1) install the library,

1)安装库

sudo apt install libss-dev

2) check the installed files,

2)检查安装的文件

dpkg-query -L libssl-dev

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

3)更改gcc标志-L(library directory) -l(library name),例如,

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

回答by renzoe

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

除了接受的答案之外,我无法编译 AES-CCM 的 OpenSSL 示例:

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

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)
  • 动态链接库: -ldl
  • 使用 POSIX 线程支持的 PThread 库:(不推荐-pthread使用 -lpthread 直接添加库)