[ Article author: 张宴 ] This article explains how to compile the Libevent 2.0.10 static link library on Windows using Microsoft Visual Studio 2005, and then use that Libevent static link library to implement a simple HTTP web server program: httpd.exe. Assume Visual Studio 2005 is installed at “D:\Program Files\Microsoft Visual Studio 8\“, and that Libevent 2.0.10 is extracted to “D:\libevent-2.0.10-stable”.
1. Compiling the Libevent 2.0.10 static link library.
Modify the three files “D:\libevent-2.0.10-stable\event_iocp.c”, “D:\libevent-2.0.10-stable \evthread_win32.c” and “D:\libevent-2.0.10-stable\listener.c”, adding the following line at the beginning of each file:
define _WIN32_WINNT 0x0500
Click 【Start】-【All Programs】 in the lower-left corner of Windows, find 【Microsoft Visual Studio 2005】, and run the script shown in the figure below:
Compile Libevent 2.0.10 using the method shown in the figure below:
The generated “libevent.lib”, “libevent_core.lib” and “libevent_extras.lib” are the Libevent static link libraries we need.

2. Using the Libevent static link library to implement a simple HTTP web server program
Choose to create a “Win32 Console Application” inside the “d:\test” directory
Once the project is created, the “d:\test\httpd\“ directory is generated automatically. Create an “httpd.c” file inside it with the following contents:
view plainprint?
#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;
}
Back in Visual Studio 2005, choose 【Add】-【Existing Item】 under 【Source Files】 on the left, and add the “httpd.c” file you created in the previous step.
Right-click on 【Solution “httpd”】 and choose 【Properties】
- Change 【Configuration】 to “Release”
- Copy the entire “D:\libevent-2.0.10-stable\include” directory to “D:\test\httpd\include”; copy the “tree.h” file and the “event2” subdirectory from “D:\libevent-2.0.10-stable\WIN32-Code” into “D: \test\httpd\include\“; copy all “*.h” header files from the “D:\libevent-2.0.10-stable\“ directory into “D: \test\httpd\include\“. You can type “cmd” in 【Start】-【Run】 in the lower-left corner of Windows and press Enter, then run the following commands in the command-line window to complete the copying.
mkdir D:\test\httpd\include\ xcopy /E /H /R D:\libevent-2.0.10-stable\include\ D:\test\httpd\include\ xcopy /E /H /R D:\libevent-2.0.10-stable\WIN32-Code\ D:\test\httpd\include\ xcopy /E /H /R D:\libevent-2.0.10-stable\*.h D:\test\httpd\include\
- Back in Visual Studio 2005, in the left-hand menu, click on 【httpd】 (the line below 【Solution “httpd”】), choose 【Properties】, and modify each item. The data inside the red boxes in the figures below is the modified data.
Note: In the figure below, fill in the additional dependencies as:
ws2\_32.lib wsock32.lib libevent.lib libevent\_core.lib libevent_extras.lib
And fill in the ignored specific libraries as:
libc.lib;msvcrt.lib;libcd.lib;libcmtd.lib;msvcrtd.lib
- Once the settings are done, right-click on 【Solution “httpd”】 and choose 【Build Solution】. If you are recompiling, you can choose 【Rebuild Solution】. After a successful build, the “httpd.exe” inside the “d:\test\httpd\Release” directory is the generated executable.
- Double-click “httpd.exe” to run it, then open a browser and visit “http://127.0.0.1:8505/“ and you will see the following information: the output of a simple HTTP web server.
- If you think the “httpd.exe” executable looks ugly with its DOS-program-like icon and shows no version information, you can follow the steps in the figures below to add an ICO icon file.
- Add version information
Appendix 1: Download the compiled Libevent 2.0.10 static link library and the Visual Studio 2005 project source code for httpd
Download file
Click here to download the file
Appendix 2: Download the ICO icon creation tool
Download file
Click here to download the file
Please indicate the original link when reposting: http://blog.zyan.cc/libevent_windows/





















