- 在官网下载包 :libevent-2.8-stable.tar.gz
- 用winscp 工具放到 ubuntu 相应的目录下,我这里放到了/home/user/code 下
- 进入到code 目录 解压 命令:tar zxvf libevent-2.1.8-stable.tar.gz
- 在code目录下新建 目录libevent 命令:mkdir libevent
- 进入 libevent-2.1.8-stable. 目录执行 : ./configure –prefix=/home/user/code/libevent
- Make
- Make install
- 如果没用报什么错误那么libevent 就已经安装好了,注意这里默认的编译器是gcc 如果要移植到arm 平台需要制定编译器
简单程序例子:
#include <stdio.h>
#include <stdio.h>
//使用libevent库所需头文件
#include <event.h>
void on_time(int sock,short event,void *arg)
{
printf("hello world\n");
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
// 事件执行后,默认就被删除,所以需要重新添加
event_add((struct event*)arg, &tv);
}
int main()
{
// 初始化事件
event_init();
// 设置定时器回调函数
struct event ev_time;
evtimer_set(&ev_time, on_time, &ev_time);
//1s运行一次func函数
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
//添加到事件循环中
event_add(&ev_time, &tv);
//程序等待就绪事件并执行事件处理
event_dispatch();
return 0;
}
执行gcc -o test test.c -levent -L /home/user/code/libevent/lib/ -I /home/user/code/libevent/include/
得到test
然后执行。