1、下载
http://libevent.org/ 目前最新版本libevent-2.0.22-stable.tar.gz
2、安装
解压;tar -zxvf libevent-2.0.22-stable.tar.gz
进入目录;
./configure --prefix=/usr 配置目录
make
make install
3.验证
root@jq-virtual-machine:/mywork/jqproject/libevent-2.0.22-stable# ls -al /usr/lib |grep libevent
lrwxrwxrwx 1 root root 21 9月 26 15:18 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 281504 12月 12 2011 libevent-2.0.so.5.1.4
-rwxr-xr-x 1 root root 827693 9月 26 15:18 libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 1159932 9月 26 15:18 libevent.a
lrwxrwxrwx 1 root root 26 9月 26 15:18 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 503577 9月 26 15:18 libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 731100 9月 26 15:18 libevent_core.a
-rwxr-xr-x 1 root root 979 9月 26 15:18 libevent_core.la
lrwxrwxrwx 1 root root 26 9月 26 15:18 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx 1 root root 27 9月 26 15:18 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 341944 9月 26 15:18 libevent_extra-2.0.so.5.1.9
-rw-r--r-- 1 root root 428904 9月 26 15:18 libevent_extra.a
-rwxr-xr-x 1 root root 986 9月 26 15:18 libevent_extra.la
lrwxrwxrwx 1 root root 27 9月 26 15:18 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 944 9月 26 15:18 libevent.la
lrwxrwxrwx 1 root root 30 9月 26 15:18 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x 1 root root 19118 9月 26 15:18 libevent_pthreads-2.0.so.5.1.9
-rw-r--r-- 1 root root 12182 9月 26 15:18 libevent_pthreads.a
-rwxr-xr-x 1 root root 1007 9月 26 15:18 libevent_pthreads.la
lrwxrwxrwx 1 root root 30 9月 26 15:18 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 9月 26 15:18 libevent.so -> libevent-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 9月 26 15:18 libevent-2.0.so.5 -> libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 281504 12月 12 2011 libevent-2.0.so.5.1.4
-rwxr-xr-x 1 root root 827693 9月 26 15:18 libevent-2.0.so.5.1.9
-rw-r--r-- 1 root root 1159932 9月 26 15:18 libevent.a
lrwxrwxrwx 1 root root 26 9月 26 15:18 libevent_core-2.0.so.5 -> libevent_core-2.0.so.5.1.9
-rwxr-xr-x 1 root root 503577 9月 26 15:18 libevent_core-2.0.so.5.1.9
-rw-r--r-- 1 root root 731100 9月 26 15:18 libevent_core.a
-rwxr-xr-x 1 root root 979 9月 26 15:18 libevent_core.la
lrwxrwxrwx 1 root root 26 9月 26 15:18 libevent_core.so -> libevent_core-2.0.so.5.1.9
lrwxrwxrwx 1 root root 27 9月 26 15:18 libevent_extra-2.0.so.5 -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 341944 9月 26 15:18 libevent_extra-2.0.so.5.1.9
-rw-r--r-- 1 root root 428904 9月 26 15:18 libevent_extra.a
-rwxr-xr-x 1 root root 986 9月 26 15:18 libevent_extra.la
lrwxrwxrwx 1 root root 27 9月 26 15:18 libevent_extra.so -> libevent_extra-2.0.so.5.1.9
-rwxr-xr-x 1 root root 944 9月 26 15:18 libevent.la
lrwxrwxrwx 1 root root 30 9月 26 15:18 libevent_pthreads-2.0.so.5 -> libevent_pthreads-2.0.so.5.1.9
-rwxr-xr-x 1 root root 19118 9月 26 15:18 libevent_pthreads-2.0.so.5.1.9
-rw-r--r-- 1 root root 12182 9月 26 15:18 libevent_pthreads.a
-rwxr-xr-x 1 root root 1007 9月 26 15:18 libevent_pthreads.la
lrwxrwxrwx 1 root root 30 9月 26 15:18 libevent_pthreads.so -> libevent_pthreads-2.0.so.5.1.9
lrwxrwxrwx 1 root root 21 9月 26 15:18 libevent.so -> libevent-2.0.so.5.1.9
4、Qt下运行实例
a、新建一个基于控制台的程序
b、.pro文件中右键->添加库->系统库,找到libevent库安装路径,我的在/usr/lib下,添加三个,最终效果如下:
unix:!macx: LIBS += -levent
unix:!macx: LIBS += -levent_core
unix:!macx: LIBS += -levent_extra
unix:!macx: LIBS += -levent_core
unix:!macx: LIBS += -levent_extra
c、main.cpp文件内容:
#include <QCoreApplication>
#include <event2/event.h>
#include <event2/event-config.h>
#include <event2/event_struct.h>
#include <QTextStream>
void timeout_cb(int sock, short event, void *arg)
{
static int i=0;
i++;
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
QString str =QString("current nuber is: %1").arg(i);
cout <<str<<endl;
cout.flush();//或者加cout.flush()才能输出
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
// 重新添加定时事件(定时事件触发后默认自动删除)
event_add((struct event*)arg, &tv);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct event timeout;
struct event_base *base;
struct timeval tv;
int flags;
// 初始化
/* Initalize the event library */
base = event_base_new();
/* Initalize one event */
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
tv.tv_sec = 1;
tv.tv_usec = 0;
// 添加定时事件
event_add(&timeout, &tv);
// 事件循环
event_base_dispatch(base);
return a.exec();
}
#include <event2/event.h>
#include <event2/event-config.h>
#include <event2/event_struct.h>
#include <QTextStream>
void timeout_cb(int sock, short event, void *arg)
{
static int i=0;
i++;
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
QString str =QString("current nuber is: %1").arg(i);
cout <<str<<endl;
cout.flush();//或者加cout.flush()才能输出
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
// 重新添加定时事件(定时事件触发后默认自动删除)
event_add((struct event*)arg, &tv);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
struct event timeout;
struct event_base *base;
struct timeval tv;
int flags;
// 初始化
/* Initalize the event library */
base = event_base_new();
/* Initalize one event */
event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
tv.tv_sec = 1;
tv.tv_usec = 0;
// 添加定时事件
event_add(&timeout, &tv);
// 事件循环
event_base_dispatch(base);
return a.exec();
}
运行效果图: