Libevent Official website: http://libevent.org/
Windows 7 under compilation:
Compiling the Environment : windows 7 + VS2010
(1) Unzip libevent to F:\libevent\libevent-2.0.21-stable
(2) Open Microsoft Visual Studio command line tool
(3) Modify the following three files to add a macro definition:
Add "#define _WIN32_WINNT 0x0500" at the beginning ofthe following 3 files
Libevent-2.0.21-stable\event_iocp.c
Libevent-2.0.21-stable\evthread_win32.c
Libevent-2.0.21-stable\listener.c
(4) compile with the VS Command prompt tool:
CD/D F:\libevent\libevent-2.0.21-stable
nmake/f Makefile.nmake
(5) Compile result:
Libevent_core.lib: Allcore event and buffer functionality. This library is contains all the Event_base, Evbuffer, bufferevent, and utility functions.
Libevent_extras.lib: Thislibrary defines protocol-specific functionality, may or may not want for your app Lication, including HTTP, DNS, and RPC.
Libevent.lib: Thislibrary exists for historical reasons; it contains the contents of both Libevent_core and Libeve Nt_extra. You shouldn ' t use it; It may go away in a future version of Libevent.
(6) using Lib under VS2010
To create a new VC + + Console project:
Environment configuration:
Project to build a Lib directory, the above three lib files to copy to the directory.
Create a new include directory, copy the files and folders under F:\libevent\libevent-2.0.21-stable\include to this directory, F:\libevent\libevent-2.0.21-stable\ Win32-code files under this directory, the files under the 2 Event2 directories can be merged together.
Main code:
//LibeventTest.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<string.h>#include<errno.h>#include<stdio.h>#include<signal.h>#ifndef win32#include<netinet/inch.h># ifdef _xopen_source_extended# include<arpa/inet.h># Endif#include<sys/socket.h>#endif#include"event2/bufferevent.h"#include"Event2/buffer.h"#include"Event2/listener.h"#include"event2/util.h"#include"event2/event.h"#include<WinSock2.h>Static Const CharMessage[] ="Hello, world!\n.";Static Const intPORT =9995;Static voidCONN_WRITECB (structBufferevent *bev,void*user_data) { structEvbuffer *output =Bufferevent_get_output (BEV); if(evbuffer_get_length (output) = =0) {printf ("Flushed answer\n"); Bufferevent_free (BEV); }}Static voidCONN_EVENTCB (structBufferevent *bev, ShortEventsvoid*user_data) { if(Events &bev_event_eof) {printf ("Connection closed.\n"); } Else if(Events &bev_event_error) {printf ("Got An error on the connection:%s\n", Strerror (errno));/*XXX Win32*/ } /*None of the other events can happen here, since we haven ' t enabled * timeouts*/Bufferevent_free (BEV);}Static voidSIGNAL_CB (evutil_socket_t Sig, ShortEventsvoid*user_data) { structEvent_base *Base= (structEvent_base *) User_data; structTimeval delay = {2,0 }; printf ("caught an interrupt signal, exiting cleanly in both seconds.\n"); Event_base_loopexit (Base, &delay);}Static voidLISTENER_CB (structEvconnlistener *Listener, evutil_socket_t FD,structSockaddr *sa,intSocklen,void*user_data) { structEvent_base *Base= (structEvent_base *) User_data; structBufferevent *Bev; Bev= Bufferevent_socket_new (Base, FD, Bev_opt_close_on_free); if(!Bev) {fprintf (stderr,"Error Constructing bufferevent!"); Event_base_loopbreak (Base); return; } BUFFEREVENT_SETCB (Bev, NULL, CONN_WRITECB, CONN_EVENTCB, NULL); Bufferevent_enable (Bev, Ev_write); Bufferevent_disable (Bev, Ev_read); Bufferevent_write (BEV, message, strlen (message));}intMainintargcChar**argv) { structEvent_base *Base; structEvconnlistener *Listener; struct Event*signal_event; structsockaddr_in sin, #ifdef WIN32 wsadata wsa_data; WSAStartup (0x0201, &wsa_data);#endif Base=event_base_new (); if(!Base) {fprintf (stderr,"Could not initialize libevent!\n"); return 1; } memset (&sin,0,sizeof(sin)); Sin.sin_family=af_inet; Sin.sin_port=htons (PORT); Listener= Evconnlistener_new_bind (Base, LISTENER_CB, (void*)Base, Lev_opt_reuseable| Lev_opt_close_on_free,-1, (structsockaddr*) &sin,sizeof(sin)); if(!listener) {fprintf (stderr,"Could not create a listener!\n"); return 1; } signal_event= Evsignal_new (Base, SIGINT, SIGNAL_CB, (void*)Base); if(!signal_event | | event_add (signal_event, NULL) <0) {fprintf (stderr,"Could not create/add a signal event!\n"); return 1; } event_base_dispatch (Base); Evconnlistener_free (listener); Event_free (signal_event); Event_base_free (Base); printf ("done\n"); return 0;}
Project Property settings:
VC + + Directory:
Include directory, add: F:\Projects\LibeventTest\LibeventTest\Include;
Library directory, add: F:\Projects\LibeventTest\LibeventTest\Lib;
C + +:
Code Generation--Runtime: Multithreaded Debugging (/MTD) (Debug), Multithreading (/MT) (under release)
Connector:
Input: Ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;
Ws2_32.lib;wsock32.lib; is used to compile Windows network-related libraries.
Compile, Build!
Compiled libevent lib download libevent2.0.21.rar
Compiling and using libevent under Windows