Libevent官网:http://libevent.org/
windows 8下编译:
编译环境: windows 8 + VS2013
(1)解压libevent到D:\libevent\libevent-2.0.21-stable
(2)打开Microsoft visual studio 2010命令行工具
(3)修改以下三个文件,添加宏定义:
在以下3个文件开头添加“#define _WIN32_WINNT 0x0500”
libevent-2.0.21-stable\event_iocp.c
libevent-2.0.21-stable\evthread_win32.c
libevent-2.0.21-stable\listener.c
(4)使用VS命令提示工具编译:
cd/d D:\libevent\libevent-2.0.21-stable
nmake /f Makefile.nmake
(5)编译结果:
libevent_core.lib:All core event and buffer functionality. This library contains all the event_base, evbuffer, bufferevent, and utility functions.
libevent_extras.lib:This library defines protocol-specific functionality that you may or may not want for your application, including HTTP, DNS, and RPC.
libevent.lib:This library exists for historical reasons; it contains the contents of both libevent_core and libevent_extra. You shouldn’t use it; it may go away in a future version of Libevent.
(6) VS2013下使用lib在工程目录下分别创建include和lib目录。
把libevent解压目录下的所有.h复制到include目录。
把lievent解压目录下的include的event目录复制到include目录。
把libevent解压目录下的WIN32-Code下的event2mulu和tree.h复制到include目录。
把libevent解压目录下编译生成的3个.lib文件(libevent.lib、libevent_core.lib和libevent_extras.lib)复制到lib目录。
// LibeventTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
// libevent头文件
#include <event.h>
using namespace std;
struct event ev; //事件
struct timeval tv; //定时器
/*事件处理函数,cb=callback*/
void time_cb(int fd, short _event, void *argc)
{
cout << "timer wakeup" << endl;
event_add(&ev, &tv);/*重新添加定时器*/
}
int _tmain(int argc, _TCHAR* argv[])
{
WSADATA WSAData; /*初始化WINDOWS socket库*/
WSAStartup(0x101, &WSAData);
struct event_base *base = event_init();//初始化libevent库,相当于初始化一个Reactior,就可以注册事件了.
tv.tv_sec = 3; //间隔
tv.tv_usec = 0;
evtimer_set(&ev, time_cb, NULL);//初始化关注的事件,并设置回调函数
//等价于event_set(&ev, -1, 0, timer_cb, NULL);
event_add(&ev, &tv);//注册事件 相当于调用Reactor::register_handler()函数注册事件
event_base_dispatch(base);//进入消息循环和消息分发
return 0;
}