下载
libevent2.0.16稳定版
https://github.com/downloads/libevent/libevent/libevent-2.0.16-stable.tar.gz
进入VS2010命令提示
d:
cd libevent-2.0.16-stable
nmake /f Makefile.nmake
编译成功生成
libevent.lib libevent_core.lib libevent_extras.lib
用vs2010 创建一个win32控制台应用程序,项目名称Httpd,取消为解决方案创建目录选项,位置d:\test
然后两个下一步,应用程序类型选择控制台应用程序,附加选项勾选空项目,其他选项空,完成。
新建Httpd.c,输入下面代码
#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", 18505);
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;
}
复制include 和libs
mkdir D:\test\httpd\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\include\* D:\test\httpd\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\WIN32-Code\* D:\test\httpd\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\*.h D:\test\httpd\include\
mkdir d:\test\httpd\libs
copy D:\libevent-2.0.16-stable\*.lib D:\test\httpd\libs\
进入配置管理器,设置活动项目为release。
设置release 的项目选项
C++
常规\附加包含目录 .\include;.\include\event2
代码生成\运行库 多线程 (/MT)
高级\编译为 编译为 C 代码 (/TC)
链接器
常规\附加库目录 .\libs
输入\附加依赖项 ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;%(AdditionalDependencies)
输入\忽略特定默认库 libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib;%(IgnoreSpecificDefaultLibraries)
注意%(AdditionalDependencies)不能漏掉,否则会报编译错误
错误 1 error LNK2001: 无法解析的外部符号 __imp__CryptGenRandom@12 D:\test\httpd\libevent.lib(evutil_rand.obj) httpd
httpd.c
F6编译成功
CTRL+F5执行程序
显示服务
httpd server start OK!
在浏览器输入
显示
Hello: /
整个Libevent2.0.16就这样成功使用了。
关于vs2005使用libevent2.0.10构建的例子,可以参考