windows下编译及使用libevent

Posted 菜b

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows下编译及使用libevent相关的知识,希望对你有一定的参考价值。

Libevent官网:http://libevent.org/

windows 7下编译:

编译环境: windows 7 + VS2010

(1)解压libevent到F:\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 F:\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)VS2010下使用lib

新建一个VC++控制台项目:

环境配置:

项目下建一个Lib目录,将上面三个lib文件copy到该目录下。

新建一个Include目录,将F:\libevent\libevent-2.0.21-stable\include下的文件和文件夹copy到该目录下,F:\libevent\libevent-2.0.21-stable\WIN32-Code下的文件copy到该目录下,2个event2目录下的文件可合并一起。

main代码:

  1 // LibeventTest.cpp : 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 
  6 #include <string.h>
  7 #include <errno.h>
  8 #include <stdio.h>
  9 #include <signal.h>
 10 
 11 #ifndef WIN32
 12 #include <netinet/in.h>
 13 # ifdef _XOPEN_SOURCE_EXTENDED
 14 #  include <arpa/inet.h>
 15 # endif
 16 #include <sys/socket.h>
 17 #endif
 18 
 19 #include "event2/bufferevent.h"
 20 #include "event2/buffer.h"
 21 #include "event2/listener.h"
 22 #include "event2/util.h"
 23 #include "event2/event.h"
 24 
 25 #include <WinSock2.h>
 26 
 27 static const char MESSAGE[] = "Hello, World!\n";
 28 
 29 static const int PORT = 9995;
 30 
 31 
 32 static void conn_writecb(struct bufferevent *bev, void *user_data)
 33 {
 34     struct evbuffer *output = bufferevent_get_output(bev);
 35     if (evbuffer_get_length(output) == 0) 
 36     {
 37         printf("flushed answer\n");
 38         bufferevent_free(bev);
 39     }
 40 }
 41 
 42 static void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
 43 {
 44     if (events & BEV_EVENT_EOF) 
 45     {
 46         printf("Connection closed.\n");
 47     } 
 48     else if (events & BEV_EVENT_ERROR) 
 49     {
 50         printf("Got an error on the connection: %s\n",
 51             strerror(errno));/*XXX win32*/
 52     }
 53     /* None of the other events can happen here, since we haven‘t enabled
 54      * timeouts */
 55     bufferevent_free(bev);
 56 }
 57 
 58 static void signal_cb(evutil_socket_t sig, short events, void *user_data)
 59 {
 60     struct event_base *base = (struct event_base *)user_data;
 61     struct timeval delay = { 2, 0 };
 62 
 63     printf("Caught an interrupt signal; exiting cleanly in two seconds.\n");
 64 
 65     event_base_loopexit(base, &delay);
 66 }
 67 
 68 static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
 69     struct sockaddr *sa, int socklen, void *user_data)
 70 {
 71     struct event_base *base = (struct event_base *)user_data;
 72     struct bufferevent *bev;
 73 
 74     bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
 75     if (!bev) 
 76     {
 77         fprintf(stderr, "Error constructing bufferevent!");
 78         event_base_loopbreak(base);
 79         return;
 80     }
 81     bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL);
 82     bufferevent_enable(bev, EV_WRITE);
 83     bufferevent_disable(bev, EV_READ);
 84 
 85     bufferevent_write(bev, MESSAGE, strlen(MESSAGE));
 86 }
 87 
 88 int main(int argc, char **argv)
 89 {
 90     struct event_base *base;
 91     struct evconnlistener *listener;
 92     struct event *signal_event;
 93 
 94     struct sockaddr_in sin;
 95 
 96 #ifdef WIN32
 97     WSADATA wsa_data;
 98     WSAStartup(0x0201, &wsa_data);
 99 #endif
100 
101     base = event_base_new();
102     if (!base) 
103     {
104         fprintf(stderr, "Could not initialize libevent!\n");
105         return 1;
106     }
107 
108     memset(&sin, 0, sizeof(sin));
109     sin.sin_family = AF_INET;
110     sin.sin_port = htons(PORT);
111 
112     listener = evconnlistener_new_bind(base, listener_cb, (void *)base,
113         LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1,
114         (struct sockaddr*)&sin,
115         sizeof(sin));
116 
117     if (!listener) 
118     {
119         fprintf(stderr, "Could not create a listener!\n");
120         return 1;
121     }
122 
123     signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base);
124 
125     if (!signal_event || event_add(signal_event, NULL)<0) 
126     {
127         fprintf(stderr, "Could not create/add a signal event!\n");
128         return 1;
129     }
130 
131     event_base_dispatch(base);
132 
133     evconnlistener_free(listener);
134     event_free(signal_event);
135     event_base_free(base);
136 
137     printf("done\n");
138     return 0;
139 }

项目属性设置:

VC++目录:

包含目录,添加:F:\Projects\LibeventTest\LibeventTest\Include;

库目录,添加:F:\Projects\LibeventTest\LibeventTest\Lib;

C/C++:

代码生成-->运行库:多线程调试 (/MTd)(Debug下),多线程 (/MT)(Release下)

连接器:

输入:ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;

ws2_32.lib;wsock32.lib;是用来编译Windows网络相关的程序库。

编译,生成!

编译好的libevent lib下载 

 

以上是关于windows下编译及使用libevent的主要内容,如果未能解决你的问题,请参考以下文章

libevent学习之二:Windows7(Win7)下编译libevent

windows下编译libevent

Windows下编译Libevent

开源库evhtp在windows下编译

Libevent库 编译与使用

CentOS下编译libev(dev)