windows下编译libevent(2.1.8)及使用

一:获取libevent

github地址:https://github.com/libevent/libevent/releases

下载2.1.8稳定版

二:编译libevent

我是用的visual studio 2010,当然也可以使用更高的版本

启动后,进入"Libevent-release-2.1.8-stable"所在的目录

直接执行会报如下错误:

按照提示分别修改:3611行,3655行内容

struct http_server hs = { .port = 0, .ssl = ssl, };
改为
struct http_server hs = { 0, ssl, };

再次编译会在目录下生成三个静态库:libevent.lib,libevent_core.lib,libevent_extras.lib 表示编译完成。

三:使用libevent实现定时器功能

vs2010新建项目,把三个静态库libevent.lib,libevent_core.lib,libevent_extras.lib,复制到工程下,把include目录复制到工程下,把WIN32-code/nmake/event2下的event-config.h头文件复制到include/event2目录下

新建main.cpp文件,内容如下:

/********************************************************
Copyright (C),  2016-2018,
FileName:		main
Author:        	woniu201
Email:         	wangpengfei.201@163.com
Created:       	2018/11/14
Description:    使用libevent实现定时器功能
********************************************************/
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/event_struct.h>
#include <ws2tcpip.h>

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "libevent.lib")
#pragma comment(lib, "libevent_core.lib")
#pragma comment(lib, "libevent_extras.lib")

static void timeout_cb(int fd, short event, void *arg)
{
	printf("run timeout_cb1\n");
}

int main()
{
#ifdef WIN32
	WORD wVersionRequested;
	WSADATA wsaData;
	wVersionRequested = MAKEWORD(2, 2);
	(void)WSAStartup(wVersionRequested, &wsaData);
#endif

	struct event_base *base; 
	base = event_base_new(); 
	
	event event1 ;
	event_assign(&event1, base, -1, EV_PERSIST, timeout_cb, (void*) &event1);

	struct timeval tv;
	tv.tv_sec = 1;
	tv.tv_usec = 0;

	event_add(&event1, &tv);
	event_base_dispatch(base);
	getchar();
	return 1;
}

posted @ 2018-11-14 14:55  蜗牛201  阅读(577)  评论(0编辑  收藏  举报