Compiling and using libevent under Windows

Source: Internet
Author: User
Tags visual studio 2010

Libevent Official website: http://libevent.org/

Windows 7 under compilation:

Compilation environment: Windows 7 + VS2010

(1) Unzip libevent to F:\libevent\libevent-2.0.21-stable

(2) Open the Microsoft Visual Studio 2010 command-line tool

(3) Modify the following three files to add a macro definition:

Add "#define _WIN32_WINNT 0x0500" at the beginning of the 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:All core event and buffer functionality. This library is contains all the Event_base, Evbuffer, bufferevent, and utility functions.

Libevent_extras.lib:This Library defines protocol-specific functionality that's 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) 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:

1 //LibeventTest.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5 6#include <string.h>7#include <errno.h>8#include <stdio.h>9#include <signal.h>Ten  One #ifndef WIN32 A#include <netinet/inch.h> - # ifdef _xopen_source_extended -# include <arpa/inet.h> the # endif -#include <sys/socket.h> - #endif -  +#include"event2/bufferevent.h" -#include"Event2/buffer.h" +#include"Event2/listener.h" A#include"event2/util.h" at#include"event2/event.h" -  -#include <WinSock2.h> -  - Static Const CharMessage[] ="Hello, world!\n."; -  in Static Const intPORT =9995; -  to  + Static voidCONN_WRITECB (structBufferevent *bev,void*user_data) - { the     structEvbuffer *output =Bufferevent_get_output (BEV); *     if(evbuffer_get_length (output) = =0)  $     {Panax Notoginsengprintf"Flushed answer\n"); - Bufferevent_free (BEV); the     } + } A  the Static voidCONN_EVENTCB (structBufferevent *bev, ShortEventsvoid*user_data) + { -     if(Events &bev_event_eof) $     { $printf"Connection closed.\n"); -     }  -     Else if(Events &bev_event_error) the     { -printf"Got An error on the connection:%s\n",WuyiStrerror (errno));/*XXX Win32*/ the     } -     /*None of the other events can happen here, since we haven ' t enabled Wu * Timeouts*/ - Bufferevent_free (BEV); About } $  - Static voidSIGNAL_CB (evutil_socket_t Sig, ShortEventsvoid*user_data) - { -     structEvent_base *Base= (structEvent_base *) User_data; A     structTimeval delay = {2,0 }; +  theprintf"caught an interrupt signal, exiting cleanly in both seconds.\n"); -  $Event_base_loopexit (Base, &delay); the } the  the Static voidLISTENER_CB (structEvconnlistener *Listener, evutil_socket_t FD, the     structSockaddr *sa,intSocklen,void*user_data) - { in     structEvent_base *Base= (structEvent_base *) User_data; the     structBufferevent *Bev; the  AboutBev = Bufferevent_socket_new (Base, FD, bev_opt_close_on_free); the     if(!Bev) the     { thefprintf (stderr,"Error Constructing bufferevent!"); +Event_base_loopbreak (Base); -         return; the     }Bayi BUFFEREVENT_SETCB (Bev, NULL, CONN_WRITECB, CONN_EVENTCB, null); the bufferevent_enable (Bev, ev_write); the bufferevent_disable (Bev, ev_read); -  - Bufferevent_write (BEV, message, strlen (message)); the } the  the intMainintargcChar**argv) the { -     structEvent_base *Base; the     structEvconnlistener *Listener; the     struct Event*signal_event; the 94     structsockaddr_in sin; the  the #ifdef WIN32 the wsadata Wsa_data;98WSAStartup (0x0201, &wsa_data); About #endif - 101     Base=event_base_new ();102     if(!Base) 103     {104fprintf (stderr,"Could not initialize libevent!\n"); the         return 1;106     }107 108memset (&sin,0,sizeof(sin));109sin.sin_family =af_inet; theSin.sin_port =htons (PORT);111  theListener = Evconnlistener_new_bind (Base, LISTENER_CB, (void*)Base,113lev_opt_reuseable| Lev_opt_close_on_free,-1, the(structsockaddr*) &sin, the         sizeof(sin)); the 117     if(!listener)118     {119fprintf (stderr,"Could not create a listener!\n"); -         return 1;121     }122 123Signal_event = Evsignal_new (Base, SIGINT, SIGNAL_CB, (void*)Base);124  the     if(!signal_event | | event_add (signal_event, NULL) <0) 126     {127fprintf (stderr,"Could not create/add a signal event!\n"); -         return 1;129     } the 131Event_base_dispatch (Base); the 133 Evconnlistener_free (listener);134 Event_free (signal_event);135Event_base_free (Base);136 137printf"done\n");138     return 0;139}

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

Compiling and using libevent under Windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.