vs2010下libevent的使用
下载最新的libevent,目前是
libevent-2.0.20-stable.tar.gz
修改“D:\libevent-2.0.10-stable\event_iocp.c”、“D:\libevent-2.0.10-stable \evthread_win32.c”、“D:\libevent-2.0.10-stable\listener.c”三个文件,在文件开头分别加上一 行:
#define _WIN32_WINNT 0x0500
在Makefile.nmake中的CFLAGS中加入/ZI选项,同时去掉/Ox优化选项,这样生成的lib库会带有调试信息 进入VS2010命令提示
切换到libevent的所在目录
nmake /f Makefile.nmake
编译成功生成
libevent.lib、libevent_core.lib、libevent_extras.lib
复制include 和libs
mkdir D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\include\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\WIN32-Code\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\*.h D:\libevent\include\
mkdir d:\libevent\libs
copy D:\libevent-2.0.16-stable\*.lib D:\libevent\libs\
xcopy /E /H /R D:\libevent-2.0.16-stable\include\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\WIN32-Code\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.16-stable\*.h D:\libevent\include\
mkdir d:\libevent\libs
copy D:\libevent-2.0.16-stable\*.lib D:\libevent\libs\
新建vs2010控制台项目并使用以下代码
#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;
}
#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;
}
进入配置管理器,设置活动项目为release
C++
常规\附加包含目录 .\include;.\include\event2
代码生成\运行库 多线程 (/MT)
高级\编译为 编译为 C 代码 (/TC)
代码生成\运行库 多线程 (/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)
输入\附加依赖项 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)