欢迎使用CSDN-markdown编辑器

Windows配置libevent

一 环境
系统:win8.1
编译器:VS2013
官方下载地址:http://libevent.org/
版本:2.0.22-stable

二 编译静态库
1 解压
把上面下载到libevent-2.0.22-stable.tar.gz解压,得到libevent-2.0.22-stable文件夹

2 添加宏定义
在libevent-2.0.22-stable文件夹下找到下面三个文件:
event_iocp.c
evthread_win32.c
listener.c
打开并在开头加上宏定义:
#define _WIN32_WINNT 0x0500
因为event_iocp.c里用到<winbase.h>头文件里的函数定义,如InitializeCriticalSectionAndSpinCount,
<windows.h>会包含<winbase.h>,而<winbase.h>这个头文件里这个函数是这样定义的:
#if (_WIN32_WINNT >= 0x0403)
WINBASEAPI
BOOL WINAPI
InitializeCriticalSectionAndSpinCount(
__out LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);

WINBASEAPI
DWORD
WINAPI
SetCriticalSectionSpinCount(
__inout LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);
#endif

所以要定义_WIN32_WINNT这个宏,而且值要大于0x0403。
如果没有这个宏或不满足条件,编译器会假定这个函数没有定义,
等到链接时再寻找它,这样这个函数的符号就假定返回一个int,
而显示标准库文件里这个函数不是返回int,所以在链接时就会找不到这个函数符号。

注意:宏一定要定义在#include <windows.h>之前,不然还是没有作用。

3 编译
使用vs的命令行工具,cd到libevent-2.0.22-stable目录,执行脚本makefile.nmake,命令如下:
nmake /f Makefile.nmake
这样就会生成三个静态库:
libevent_core.lib
libevent_extras.lib
libevent.lib

三 使用示例

1 新建项目

新建一个控制台“空”项目

2 拷贝文件
2.1 在项目目录下建一个libevent文件夹
2.2 在libevent中新建一个lib文件夹,将上面三个lib文件copy到该目录下。
2.3 在libevent中再新建一个include文件夹,
将libevent-2.0.22-stable\include下的文件和文件夹copy到该目录下,
将libevent-2.0.22-stable\WIN32-Code下的文件和文件夹copy到该目录下,
2个event2目录下的文件合并一起。

3 项目配置
VC++目录:
包含目录,添加刚刚新建的include目录
库目录,添加刚刚的lib目录;

C/C++:
代码生成–>运行库:
Debug模式下选:多线程调试 (/MTd),
Release下模式下选:多线程 (/MT)
预处理器: Preprocessor Definitions添加”WIN32”

连接器:
输入->附加依赖项:
ws2_32.lib
wsock32.lib
libevent.lib
libevent_core.lib
libevent_extras.lib
另外两个库ws2_32.lib和wsock32.lib是用来编译Windows网络相关的程序库。

4 测试代码
4.1 新建一个main.c文件
4.2 从libevent-2.0.22-stable\sample目录下拷贝time-test.c文件中的代码到main中,代码如下:



#include <event2/event-config.h>


#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>


#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>


#ifdef WIN32
#include <winsock2.h>
#endif




struct timeval lasttime;


int event_is_persistent;


static void timeout_cb(evutil_socket_t fd, short event, void *arg)
{
    struct timeval newtime, difference;
    struct event *timeout = arg;
    double elapsed;


    evutil_gettimeofday(&newtime, NULL);
    evutil_timersub(&newtime, &lasttime, &difference);
    elapsed = difference.tv_sec +
        (difference.tv_usec / 1.0e6);


    printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
        (int)newtime.tv_sec, elapsed);
    lasttime = newtime;


    if (!event_is_persistent) {
        struct timeval tv;
        evutil_timerclear(&tv);
        tv.tv_sec = 2;
        event_add(timeout, &tv);
    }
}


int main(int argc, char **argv)
{
    struct event timeout;
    struct timeval tv;
    struct event_base *base;
    int flags;


#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;


    wVersionRequested = MAKEWORD(2, 2);


    (void)WSAStartup(wVersionRequested, &wsaData);
#endif


    if (argc == 2 && !strcmp(argv[1], "-p")) {
        event_is_persistent = 1;
        flags = EV_PERSIST;
    }
    else {
        event_is_persistent = 0;
        flags = 0;
    }


    /* Initalize the event library */
    base = event_base_new();


    /* Initalize one event */
    event_assign(&timeout, base, -1, flags, timeout_cb, (void*)&timeout);


    evutil_timerclear(&tv);
    tv.tv_sec = 2;
    event_add(&timeout, &tv);


    evutil_gettimeofday(&lasttime, NULL);


    event_base_dispatch(base);


    return (0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值