libevent_windows下使用方法—vs2005

1. 编译libevent-2.0.12-stable

打开vs中的dos工具,进入到libevent-2.0.12-stable所在目录,然后运行nmake /f Makefile.nmake.

编译完成后,libevent-2.0.12-stable目录中生成三个静态库libevent.lib libevent_core.lib libevent_extras.lib

2. 将库文件导入工程:

新建工程后,在工程中建一个include文件夹,将libevent-2.0.12-stable\include中的所有文件复制到工程中的include中,将libevent-2.0.12-stable\WIN32-Code中的“tree.h”和“event2”子目录复制到工程中新建的include中,最后将libevent-2.0.12-stable中的所有*.h头文件复制到工程中的include中。

也可以在【开始】-》【运行】-cmddos中执行

xopy /E /H /R libevent-2.0.12-stable\include\*  project\include\

xopy /E /H /R libevent-2.0.12-stable\WIN32-Code\*  project\include\

xopy /E /H /R libevent-2.0.12-stable\*.h  project\include\

 

(A) vs设置

1. 将include加入到工程中:

 


2. 设置运行时库:

 

 

3,选择语言c/c++,我觉得c++也应该可以

 

4.添加库到工程中:

 

5.添加依赖和忽略特定库:

附加依赖库为:ws2_32.lib wsock32.lib libevent.lib libevent_core.lib libevent_extras.lib

忽略特定库为:libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib

 

最后,libevent搭建成功,可以些代码,测试代码为

//#include "test.h"

 

#include <stdio.h>   

  

#define WIN32_LEAN_AND_MEAN   

#include <windows.h>   

#include <winsock2.h>   

  

#include <event.h>   

#include <evhttp.h>   

  

void root_handler(struct evhttp_request *req, void *arg)   

{   

    struct evbuffer *buf = evbuffer_new();   

    if(!buf){   

        puts("failed to create response buffer");   

        return;   

    }   

  

    evbuffer_add_printf(buf, "Hello: %s\n", evhttp_request_uri(req));   

    evhttp_send_reply(req, HTTP_OK, "OK", buf);   

}   

  

void generic_handler(struct evhttp_request *req, void *arg)   

{   

    struct evbuffer *buf = evbuffer_new();   

    if(!buf){   

        puts("failed to create response buffer");   

        return;   

    }   

  

    evbuffer_add_printf(buf, "Requested: %s\n", evhttp_request_uri(req));   

    evhttp_send_reply(req, HTTP_OK, "OK", buf);   

}   

  

int main(int argc, wchar_t* argv[])   

{   

  struct evhttp *httpd;   

  

  WSADATA wsaData;   

  DWORD Ret;   

  if ((Ret = WSAStartup(MAKEWORD(2, 2), &wsaData)) != 0)  {   

    printf("WSAStartup failed with error %d\n", Ret);   

    return -1;   

  }   

  

    event_init();   

  

    httpd = evhttp_start("0.0.0.0", 8505);   

    if(!httpd){   

    return 1;   

  }   

  

    evhttp_set_cb(httpd, "/", root_handler, NULL);   

    evhttp_set_gencb(httpd, generic_handler, NULL);   

  

  printf("httpd server start OK!\n");   

  

    event_dispatch();   

  

    evhttp_free(httpd);   

  

  WSACleanup();   

    return 0;   

}   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值