转自:https://blog.csdn.net/xufeng0991/article/details/44134005
libevent是一个常用的网络库,下面就看看在windows下面编译测试的过程吧。
一 环境
系统:win8.1编译器:VS2013
官方下载地址:http://libevent.org/
版本:2.0.22-stable
二 编译静态库
1 解压把上面下载到libevent-2.0.22-stable.tar.gz解压,得到libevent-2.0.22-stable文件夹
2 添加宏定义
在libevent-2.0.22-stable文件夹下找到下面三个文件:
event_iocp.c
evthread_win32.c
listener.c
打开并在开头加上宏定义:
#define _WIN32_WINNT 0x0500
因为event_iocp.c里用到<winbase.h>头文件里的函数定义,如InitializeCriticalSectionAndSpinCount,
<windows.h>会包含<winbase.h>,而<winbase.h>这个头文件里这个函数是这样定义的:
#if (_WIN32_WINNT >= 0x0403)
WINBASEAPI
BOOL WINAPI
InitializeCriticalSectionAndSpinCount(
__out LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);
WINBASEAPI
DWORD
WINAPI
SetCriticalSectionSpinCount(
__inout LPCRITICAL_SECTION lpCriticalSection,
__in DWORD dwSpinCount
);
#endif
所以要定义_WIN32_WINNT这个宏,而且值要大于0x0403。
如果没有这个宏或不满足条件,编译器会假定这个函数没有定义,
等到链接时再寻找它,这样这个函数的符号就假定返回一个int,
而显示标准库文件里这个函数不是返回int,所以在链接时就会找不到这个函数符号。
注意:宏一定要定义在#include <windows.h>之前,不然还是没有作用。
3 编译
使用vs的命令行工具,cd到libevent-2.0.22-stable目录,执行脚本makefile.nmake,命令如下:
nmake /f Makefile.nmake
这样就会生成三个静态库:
libevent_core.lib
libevent_extras.lib
libevent.lib
需要注意的是,在我使用的libevent是2.1.8版本里面的test文件夹缺少print-winsock-errors.c,使得编译会出错,可以在https://github.com/libevent/libevent/tree/patches-2.1下载其他的libevent版本,把里面的print-winsock-errors.c复制到test文件夹里,这样便可以编译成功。具体的print-winsock-errors.c附在文后。
并且啊,真的坑死人。libevent默认编译的是release版本的,如果需要编译debug版本的,修改一下makefile.nmake:
#CFLAGS=$(CFLAGS) /Ox /W3 /wd4996 /nologo
修改为:
#CFLAGS=$(CFLAGS) /D_DEBUG /Od /W3 /wd4996 /nologo
三 使用示例
1 新建项目
新建一个控制台“空”项目
2 拷贝文件
2.1 在项目目录下建一个libevent文件夹2.2 在libevent中新建一个lib文件夹,将上面三个lib文件copy到该目录下。
2.3 在libevent中再新建一个include文件夹,
将libevent-2.0.22-stable\include下的文件和文件夹copy到该目录下,
将libevent-2.0.22-stable\WIN32-Code下的文件和文件夹copy到该目录下,
2个event2目录下的文件合并一起。
3 项目配置
VC++目录:包含目录,添加刚刚新建的include目录
库目录,添加刚刚的lib目录;
C/C++:
代码生成-->运行库:
Debug模式下选:多线程调试 (/MTd),
Release下模式下选:多线程 (/MT)
连接器:
输入->附加依赖项:
ws2_32.lib
wsock32.lib
libevent.lib
libevent_core.lib
libevent_extras.lib
另外两个库ws2_32.lib和wsock32.lib是用来编译Windows网络相关的程序库。
4 测试代码
4.1 新建一个main.c文件4.2 从libevent-2.0.22-stable\sample目录下拷贝time-test.c文件中的代码到main中,代码如下:
#include <sys/types.h>
#include <event2/event-config.h>
#include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h>
#include <unistd.h>
#endif
#include <time.h>
#ifdef _EVENT_HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>
#ifdef WIN32
#include <winsock2.h>
#endif
struct timeval lasttime;
int event_is_persistent;
static void timeout_cb(evutil_socket_t fd, short event, void *arg)
{
struct timeval newtime, difference;
struct event *timeout = arg;
double elapsed;
evutil_gettimeofday(&newtime, NULL);
evutil_timersub(&newtime, &lasttime, &difference);
elapsed = difference.tv_sec +
(difference.tv_usec / 1.0e6);
printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
(int)newtime.tv_sec, elapsed);
lasttime = newtime;
if (!event_is_persistent) {
struct timeval tv;
evutil_timerclear(&tv);
tv.tv_sec = 2;
event_add(timeout, &tv);
}
}
int main(int argc, char **argv)
{
struct event timeout;
struct timeval tv;
struct event_base *base;
int flags;
#ifdef WIN32
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
(void)WSAStartup(wVersionRequested, &wsaData);
#endif
if (argc == 2 && !strcmp(argv[1], "-p")) {
event_is_persistent = 1;
flags = EV_PERSIST;
}
else {
event_is_persistent = 0;
flags = 0;
}
/* Initalize the event library */
base = event_base_new();
/* Initalize one event */
event_assign(&timeout, base, -1, flags, timeout_cb, (void*)&timeout);
evutil_timerclear(&tv);
tv.tv_sec = 2;
event_add(&timeout, &tv);
evutil_gettimeofday(&lasttime, NULL);
event_base_dispatch(base);
return (0);
}
libevent编译文件里需要的文件test\print-winsock-errors.c:
#include <winsock2.h>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "event2/event.h"
#include "event2/util.h"
#include "event2/thread.h"
#define E(x) printf (#x " -> \"%s\"\n", evutil_socket_error_to_string (x));
int main (int argc, char **argv)
{
int i, j;
const char *s1, *s2;
#ifdef EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED
evthread_use_windows_threads ();
#endif
s1 = evutil_socket_error_to_string (WSAEINTR);
for (i = 0; i < 3; i++) {
printf ("\niteration %d:\n\n", i);
E(WSAEINTR);
E(WSAEACCES);
E(WSAEFAULT);
E(WSAEINVAL);
E(WSAEMFILE);
E(WSAEWOULDBLOCK);
E(WSAEINPROGRESS);
E(WSAEALREADY);
E(WSAENOTSOCK);
E(WSAEDESTADDRREQ);
E(WSAEMSGSIZE);
E(WSAEPROTOTYPE);
E(WSAENOPROTOOPT);
E(WSAEPROTONOSUPPORT);
E(WSAESOCKTNOSUPPORT);
E(WSAEOPNOTSUPP);
E(WSAEPFNOSUPPORT);
E(WSAEAFNOSUPPORT);
E(WSAEADDRINUSE);
E(WSAEADDRNOTAVAIL);
E(WSAENETDOWN);
E(WSAENETUNREACH);
E(WSAENETRESET);
E(WSAECONNABORTED);
E(WSAECONNRESET);
E(WSAENOBUFS);
E(WSAEISCONN);
E(WSAENOTCONN);
E(WSAESHUTDOWN);
E(WSAETIMEDOUT);
E(WSAECONNREFUSED);
E(WSAEHOSTDOWN);
E(WSAEHOSTUNREACH);
E(WSAEPROCLIM);
E(WSASYSNOTREADY);
E(WSAVERNOTSUPPORTED);
E(WSANOTINITIALISED);
E(WSAEDISCON);
E(WSATYPE_NOT_FOUND);
E(WSAHOST_NOT_FOUND);
E(WSATRY_AGAIN);
E(WSANO_RECOVERY);
E(WSANO_DATA);
E(0xdeadbeef); /* test the case where no message is available */
/* fill up the hash table a bit to make sure it grows properly */
for (j = 0; j < 50; j++) {
int err;
evutil_secure_rng_get_bytes(&err, sizeof(err));
evutil_socket_error_to_string(err);
}
}
s2 = evutil_socket_error_to_string (WSAEINTR);
if (s1 != s2)
printf ("caching failed!\n");
libevent_global_shutdown ();
return EXIT_SUCCESS;
}
扫描二维码关注公众号,回复:
2261047 查看本文章
![](/qrcode.jpg)