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中。
也可以在【开始】-》【运行】-【cmd】dos中执行
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;
}