构建基于OpenSSL的应用程序需要OpenSSL库。在尝试使用gcc与openssl库和函数构建时,使用了-lssl选项。但你可能会犯这样的错误 福。cpp:21:25:错误:openssl/bio.h:没有这样的文件或目录 “这意味着找不到或没有安装openssl库。在本教程中,我们将学习如何在Ubuntu、Debian和Mint中安装OpenSSL库来构建应用程序。
OpenSSL标头和生成错误
OpenSSL提供了许多安全特性。它们都被分类在不同的C头文件下。下面我们列出一些最流行的OpenSSL库头。如果您需要其中一个,那么应该为当前操作系统安装OpenSSL库。
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/des.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
#include <openssl/rsa.h>
如果在尝试构建具有OpenSSL库依赖关系的C应用程序时未安装OpenSSL开发库,则会出现以下错误。可以使用以下gcc命令启动OpenSSL库的构建操作。
gcc -lssl sample.cpp
sample.cpp:21:25: error: openssl/bio.h: No such file or directory
sample.cpp:22:28: error: openssl/buffer.h: No such file or directory
sample.cpp:23:25: error: openssl/des.h: No such file or directory
sample.cpp:24:25: error: openssl/evp.h: No such file or directory
sample.cpp:25:25: error: openssl/pem.h: No such file or directory
sample.cpp:26:25: error: openssl/rsa.h: No such file or directory
安装OpenSSL开发库和包
OpenSSL开发库和包的名称为“libssl dev”。“libssl dev”是Ubuntu、Debian、Mint和相关的基于apt的发行版的包名。在安装OpenSSL开发库和包之前,我们可以显示有关它的信息。
apt show libssl-dev
Package: libssl-dev Version: 1.1.1f-1ubuntu4.3 Priority: optional Section: libdevel Source: openssl Origin: Ubuntu Maintainer: Ubuntu Developers [email protected] Original-Maintainer: Debian OpenSSL Team [email protected] Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 8.050 kB Depends: libssl1.1 (= 1.1.1f-1ubuntu4.3) Suggests: libssl-doc Conflicts: libssl1.0-dev Homepage: https://www.openssl.org/ Download-Size: 1.586 kB APT-Manual-Installed: yes APT-Sources: http://tr.archive.ubuntu.com/ubuntu groovy-updates/main amd64 Packages Description: Secure Sockets Layer toolkit - development files This package is part of the OpenSSL project's implementation of the SSL and TLS cryptographic protocols for secure communication over the Internet. . It contains development libraries, header files, and manpages for libssl and libcrypto. N: There is 1 additional record. Please use the '-a' switch to see it
通过使用以下命令,可以安装OpenSSL开发库和包。
sudo apt install libssl-dev
![图片[1]-如何在Ubuntu,Debian,Mint上安装OpenSSL库?-yiteyi-C++库](https://www.yiteyi.com/wp-content/uploads/2021/05/linuxtect_image-5.png)
包名“libssl”是包的名称,“dev”用于指定此包是开发库。
相关文章: Linux zgrep命令教程
如果您使用apt get命令而不是apt来管理包,那么可以使用以下命令来安装OpenSSQL开发库和包。
sudo apt-get install libssl-dev
从源代码安装OpenSSL开发库
或者,可以从源代码安装OpenSSL开发库。首先源代码应该编译成二进制,然后正确安装。使用源代码的好处是我们可以选择任何我们想要的版本。不依赖于Linux发行版提供的特定版本。从源代码安装OpenSSL develpoment库的缺点是,它不像用package manager安装那样容易。这有多个步骤。
从OpenSSL.org下载OpenSSL源代码。你可以选择任何你想要的版本。在本例中,我们下载openssl-1.0.1。
wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
使用tar命令提取压缩的openssl源代码。
tar -xvzf openssl-1.0.1g.tar.gz
浏览提取的openssl目录。
cd openssl-1.0.1g
为编译操作配置openssl源代码。
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl
使用make命令编译openssl和开发库。
make
使用“makeinstall”命令安装已编译的openssl二进制文件和开发库。
make install
导航到OpenSSL安装目录并执行OpenSSL命令以检查其版本。
/usr/local/openssl/bin/openssl version
输出如下。
OpenSSL 1.0.1g 2015年4月7日