Ubuntu安装libevent库

Libevent是一个高性能事件通知库,它在网络编程中非常有用。它可以用于开发各种网络应用程序,包括服务器和客户端。在本篇博客中,我们将介绍如何在Linux系统上安装libevent库,以便你可以开始编写高性能的网络应用程序。

1.准备工作

在开始安装libevent之前,确保你的Linux系统已经安装了以下工具和库:

  • gcc编译器
  • make工具

2.下载libevent

你可以从libevent的官方网站(https://libevent.org/)下载最新版本的libevent。在终端中,使用`wget`命令下载libevent的源代码压缩包:

wget https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz

也可以下载好安装包用XFTP传输过去

3.解压并编译libevent

解压下载的libevent源代码:

1
tar zxvf libevent-2.1.12-stable.tar.gz 

image-20241216114409549

跳转到相应的目录

1
cd libevent-2.1.12-stable

接下来,运行configure脚本以配置libevent的编译选项

1
./configure

image-20241216114555148

最后,使用make命令编译libevent:

1
make

image-20241216114625265

4.安装libevent

在编译完成后,你可以使用以下命令安装libevent:

1
sudo make install

一定要加sudo

image-20241216114700908

5.验证安装

1
cd sample

这里有很多的测试案例,挑选一个进行测试,笔者选的是hello-world

1
gcc hello-world.c -o hello -levent

能编译成功就说明安装好了

image-20241216114802391

运行程序结果图

image-20241216115030345

也可以按照如图所示去查看一下有没有libevent库

image-20241217171902632

6.可能遇到的错误

configure: error: openssl is a must but can not be found. You should add the directory containing openssl.pc' to the PKG_CONFIG_PATH’ environment variable, or set CFLAGS' and LDFLAGS’ directly for openssl, or use `–disable-openssl’ to disable support for openssl encryption

报错解释:

这个错误表明libevent在配置阶段缺失了对openssl的依赖。OpenSSL是一个强大的安全套接字层库,它用于实现网络通讯过程中的加密。libevent需要OpenSSL来支持SSL连接,因此在编译libevent之前需要确保OpenSSL库在系统中已经安装。

解决方法:

安装OpenSSL库

1
sudo apt-get install libssl-dev