[Turn]windows to compile and use libevent

Source: Internet
Author: User
Tags sin visual studio 2010

Http://www.cnblogs.com/luxiaoxun/p/3603399.html

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:

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/in.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 char message[] =" Hello, world!\n "; static const int PORT = 9995;static void Conn_writecb (struct bufferevent *bev, void *user_data) {Struc    T Evbuffer *output = bufferevent_get_output (BEV);        if (evbuffer_get_length (output) = = 0) {printf ("flushed answer\n");    Bufferevent_free (BEV);     }}static void Conn_eventcb (struct bufferevent *bev, short events, void *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 E Vents can happen here, since we haven ' t enabled * timeouts */Bufferevent_free (BEV);}  static void Signal_cb (evutil_socket_t sig, short events, void *user_data) {struct Event_base *base = (struct event_base    *) User_data;    struct Timeval delay = {2, 0}; printf ("caught an interrupt signal;    Exiting cleanly in seconds.\n "); Event_base_loopexit (base, &delay);} static void Listener_cb (struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *sa, int socklen, void *us    Er_data) {struct Event_base *base = (struct event_base *) user_data;    struct Bufferevent *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));}    int main (int argc, char **argv) {struct event_base *base;    struct Evconnlistener *listener;    struct event *signal_event;    struct sockaddr_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, (struct sockaddr*) &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

[Turn]windows to compile and use libevent

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.