libevent在windows下使用步驟詳解

來源:互聯網
上載者:User

標籤:libevent

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

三 使用樣本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>#endifstruct 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 WIN32WORD wVersionRequested;WSADATA wsaData;wVersionRequested = MAKEWORD(2, 2);(void)WSAStartup(wVersionRequested, &wsaData);#endifif (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);}

4.3 編譯運行結果:



libevent在windows下使用步驟詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.