官网下载:https://libevent.org/
直接地址:https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
解压后编译,遇到如下问题,重装openssl也不行:
[root@localhost libevent-2.1.12-stable]# ./configure
此处省略...
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
[root@localhost libevent-2.1.12-stable]# make
make: *** 没有指明目标并且找不到 makefile。 停止。
[root@localhost libevent-2.1.12-stable]#
直接按照提示加了–disable-openssl就可以编译了
配置:
./configure --disable-openssl
如需要可以指定目录,则后续编译请指定库目录:
./configure -prefix=/usr/lib/libevent --disable-openssl
编译:
make && make install
查看安装与否:
ls -al /usr/lib | grep libevent
注意:如果编译时,发现找不到定义,请查看环境变量问题
测试 用例:
#include <iostream>
#include <event.h>
#include <time.h>
struct event ev;
struct timeval tv;
void timer_cb(int fd, short envent, void *arg)
{
std::cout << "timer_cb" << std::endl;
event_add(&ev, &tv);
}
int main()
{
struct event_base *base = event_init();
tv.tv_sec = 1;
tv.tv_usec = 0;
event_set(&ev, -1, 0, timer_cb, NULL);
event_base_set(base, &ev);
event_add(&ev, &tv);
event_base_dispatch(base);
return 0;
}
编译(将源码中的头文件拷贝到测试代码的目录):
[root@localhost test]# ls
a.out compile.sh include libeventTest.cpp
[root@localhost test]# cat compile.sh
#!/bin/sh
g++ --static -I ./include libeventTest.cpp -levent
[root@localhost test]#
学习文档:https://www.bookstack.cn/read/libevent/450ef2232c710e15.md