在Windows vs2015環境下編譯使用Libevent

libevent是一個經常使用的網絡庫,下面就看看在windows下面編譯測試的過程吧。windows

環境網絡

系統:win10
編譯器:VS2015
官方下載地址:http://libevent.org/
版本:2.0.22-stable多線程

編譯靜態庫socket

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函數

使用示例工具

1 新建項目測試

新建一個控制檯「空」項目spa

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中,代碼以下:

  1 #include <sys/types.h>  
  2   
  3   
  4 #include <event2/event-config.h>  
  5   
  6   
  7 #include <sys/stat.h>  
  8 #ifndef WIN32  
  9 #include <sys/queue.h>  
 10 #include <unistd.h>  
 11 #endif  
 12 #include <time.h>  
 13 #ifdef _EVENT_HAVE_SYS_TIME_H  
 14 #include <sys/time.h>  
 15 #endif  
 16 #include <fcntl.h>  
 17 #include <stdlib.h>  
 18 #include <stdio.h>  
 19 #include <string.h>  
 20 #include <errno.h>  
 21   
 22   
 23 #include <event2/event.h>  
 24 #include <event2/event_struct.h>  
 25 #include <event2/util.h>  
 26   
 27   
 28 #ifdef WIN32  
 29 #include <winsock2.h>  
 30 #endif  
 31   
 32   
 33   
 34   
 35 struct timeval lasttime;  
 36   
 37   
 38 int event_is_persistent;  
 39   
 40   
 41 static void timeout_cb(evutil_socket_t fd, short event, void *arg)  
 42 {  
 43     struct timeval newtime, difference;  
 44     struct event *timeout = arg;  
 45     double elapsed;  
 46   
 47   
 48     evutil_gettimeofday(&newtime, NULL);  
 49     evutil_timersub(&newtime, &lasttime, &difference);  
 50     elapsed = difference.tv_sec +  
 51         (difference.tv_usec / 1.0e6);  
 52   
 53   
 54     printf("timeout_cb called at %d: %.3f seconds elapsed.\n",  
 55         (int)newtime.tv_sec, elapsed);  
 56     lasttime = newtime;  
 57   
 58   
 59     if (!event_is_persistent) {  
 60         struct timeval tv;  
 61         evutil_timerclear(&tv);  
 62         tv.tv_sec = 2;  
 63         event_add(timeout, &tv);  
 64     }  
 65 }  
 66   
 67   
 68 int main(int argc, char **argv)  
 69 {  
 70     struct event timeout;  
 71     struct timeval tv;  
 72     struct event_base *base;  
 73     int flags;  
 74   
 75   
 76 #ifdef WIN32  
 77     WORD wVersionRequested;  
 78     WSADATA wsaData;  
 79   
 80   
 81     wVersionRequested = MAKEWORD(2, 2);  
 82   
 83   
 84     (void)WSAStartup(wVersionRequested, &wsaData);  
 85 #endif  
 86   
 87   
 88     if (argc == 2 && !strcmp(argv[1], "-p")) {  
 89         event_is_persistent = 1;  
 90         flags = EV_PERSIST;  
 91     }  
 92     else {  
 93         event_is_persistent = 0;  
 94         flags = 0;  
 95     }  
 96   
 97   
 98     /* Initalize the event library */  
 99     base = event_base_new();  
100   
101   
102     /* Initalize one event */  
103     event_assign(&timeout, base, -1, flags, timeout_cb, (void*)&timeout);  
104   
105   
106     evutil_timerclear(&tv);  
107     tv.tv_sec = 2;  
108     event_add(&timeout, &tv);  
109   
110   
111     evutil_gettimeofday(&lasttime, NULL);  
112   
113   
114     event_base_dispatch(base);  
115   
116   
117     return (0);  
118 }

運行截圖:

相關文章
相關標籤/搜索