windows下编译及使用libevent | 少将全栈
  • 欢迎访问少将全栈,学会感恩,乐于付出,珍惜缘份,成就彼此、推荐使用最新版火狐浏览器和Chrome浏览器访问本网站。
  • 吐槽,投稿,删稿,交个朋友
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏少将全栈吧

windows下编译及使用libevent

点滴 admin 10年前 (2014-12-16) 3651次浏览 已收录 扫描二维码

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

windows 7下编译:

编译环境 windows 7 + VS2010

(1)解压libevent到F:libeventlibevent-2.0.21-stable (请根据你的路径做相关修改)

(2)打开Microsoft visual studio 2010命令行工具


(3)修改以下三个文件,添加宏定义:(此处不修改编译通过)

在以下3个文件开头添加“#define _WIN32_WINNT 0x0500

libevent-2.0.21-stableevent_iocp.c

libevent-2.0.21-stableevthread_win32.c

libevent-2.0.21-stablelistener.c

(4)使用VS命令提示工具编译:

cd/d F:libeventlibevent-2.0.21-stable

nmake /f Makefile.nmake

(5)编译结果:

libevent_core.libAll core event and buffer functionality. This library contains all the event_base, evbuffer, bufferevent, and utility functions.

libevent_extras.libThis library defines
protocol-specific functionality that you may or may not want for your
application, including HTTP, DNS, and RPC.

libevent.libThis 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:libeventlibevent-2.0.21-stableinclude下的文件和文
件夹copy到该目录下,F:libeventlibevent-2.0.21-stableWIN32-Code下的文件copy到该目录下,2
个event2目录下的文件可合并一起。

main代码:


// LibeventTest.cpp : 定义控制台应用程序的入口点。
//

#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!
";

static const int PORT = 9995;


static void conn_writecb(struct bufferevent *bev, void *user_data)
{
    struct evbuffer *output = bufferevent_get_output(bev);
    if (evbuffer_get_length(output) == 0) 
    {
        printf("flushed answer
");
        bufferevent_free(bev);
    }
}

static void conn_eventcb(struct bufferevent *bev, short events, void *user_data)
{
    if (events & BEV_EVENT_EOF) 
    {
        printf("Connection closed.
");
    } 
    else if (events & BEV_EVENT_ERROR) 
    {
        printf("Got an error on the connection: %s
",
            strerror(errno));/*XXX win32*/
    }
    /* None of the other events 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 two seconds.
");

    event_base_loopexit(base, &delay);
}

static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd,
    struct sockaddr *sa, int socklen, void *user_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!
");
        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!
");
        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!
");
        return 1;
    }

    event_base_dispatch(base);

    evconnlistener_free(listener);
    event_free(signal_event);
    event_base_free(base);

    printf("done
");
    return 0;
}



LibeventTest文件


项目属性设置:

VC++目录:

包含目录,添加:F:ProjectsLibeventTestLibeventTestInclude;

库目录,添加:F:ProjectsLibeventTestLibeventTestLib;

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下载 Libevent2.0.21.rar

 

这个是很久前跟xsky玩xblog的时候关注的libevent。

自己亲自测试过,也没什么难度,不同平台可能报不同错,根据错误提示做相关修改就行,

(以上源码不同平台需要细微修改。)

现在的环境是Win10 64位 +  Vs2015

刚刚下载了最新的libevent-2.0.22-stable

windows下编译及使用libevent

编译成功,这里最关键的就是环境的设置(包含目录,库环境等),遇到问题还是自己多调试吧,编译后的结果如下,因为没有玩xblog程序了,

原本计划为xblog增加功能,个人精力有限,就没有时间研究那么多了,目前主要学习精力放在前端开发,移动web app。(这个教程已经很详细了,

看了下,并且有给测试结果,如果还不会的话,麻烦多搜索下了。)

windows下编译及使用libevent

作者:阿凡卢
出处:http://www.cnblogs.com/luxiaoxun/p/3603399.html

喜欢 (0)
[🍬谢谢你请我吃糖果🍬🍬~]
分享 (0)
关于作者:
少将,关注Web全栈开发、项目管理,持续不断的学习、努力成为一个更棒的开发,做最好的自己,让世界因你不同。