標籤:os io 使用 strong ar for 檔案 div cti
下載最新的libevent,目前是
libevent-2.0.21-stable.tar.gz
修改“D:\libevent-2.0.21-stable\event_iocp.c”、“D:\libevent-2.0.21-stable \evthread_win32.c”、“D:\libevent-2.0.21-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.21-stable\include\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.21-stable\WIN32-Code\* D:\libevent\include\
xcopy /E /H /R D:\libevent-2.0.21-stable\*.h D:\libevent\include\
mkdir d:\libevent\libs
copy D:\libevent-2.0.21-stable\*.lib D:\libevent\libs\
建立vs2010控制台項目並使用以下代碼
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
// libeventTest.cpp : Defines the entry point for the console application.// #include "stdafx.h"/* * XXX This sample code was once meant to show how to use the basic Libevent * interfaces, but it never worked on non-Unix platforms, and some of the * interfaces have changed since it was first written. It should probably * be removed or replaced with something better. * * Compile with: * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent */ #include <event2/event-config.h> #include <sys/types.h>#include <sys/stat.h>#ifndef WIN32#include <sys/queue.h>#include <unistd.h>#include <sys/time.h>#else#include <winsock2.h>#include <windows.h>#endif#include <fcntl.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <errno.h> #include <event.h> static voidfifo_read(int fd, short event, void *arg){char buf[255];int len;struct event *ev = (struct event *)arg;#ifdef WIN32DWORD dwBytesRead;#endif /* Reschedule this event */event_add(ev, NULL); fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",fd, event, arg);#ifdef WIN32len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL); /* Check for end of file. */if (len && dwBytesRead == 0) {fprintf(stderr, "End Of File");event_del(ev);return;} buf[dwBytesRead] = ‘\0‘;#elselen = read(fd, buf, sizeof(buf) - 1); if (len == -1) {perror("read");return;} else if (len == 0) {fprintf(stderr, "Connection closed\n");return;} buf[len] = ‘\0‘;#endiffprintf(stdout, "Read: %s\n", buf);} intmain(int argc, char **argv){struct event evfifo;#ifdef WIN32HANDLE socket;/* Open a file. */socket = CreateFileA("test.txt", /* open File */GENERIC_READ, /* open for reading */0, /* do not share */NULL, /* no security */OPEN_EXISTING, /* existing file only */FILE_ATTRIBUTE_NORMAL, /* normal file */NULL); /* no attr. template */ if (socket == INVALID_HANDLE_VALUE)return 1; #elsestruct stat st;const char *fifo = "event.fifo";int socket; if (lstat(fifo, &st) == 0) {if ((st.st_mode & S_IFMT) == S_IFREG) {errno = EEXIST;perror("lstat");exit(1);}} unlink(fifo);if (mkfifo(fifo, 0600) == -1) {perror("mkfifo");exit(1);} /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */#ifdef __linuxsocket = open(fifo, O_RDWR | O_NONBLOCK, 0);#elsesocket = open(fifo, O_RDONLY | O_NONBLOCK, 0);#endif if (socket == -1) {perror("open");exit(1);} fprintf(stderr, "Write data to %s\n", fifo);#endif/* Initalize the event library */event_init(); /* Initalize one event */#ifdef WIN32event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo);#elseevent_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);#endif /* Add it to the active events, without a timeout */event_add(&evfifo, NULL); event_dispatch();#ifdef WIN32CloseHandle(socket);#endifreturn (0);} |
進入組態管理員,設定活動項目為release
C++常規\附加元件封裝含目錄 .\include;.\include\event2
代碼產生\運行庫 多線程 (/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)
haven2002標註:不用忽略也沒事
include檔案夾下面,包含event2檔案夾和一堆.h檔案即可。用上面的命令,複製了一堆沒用的東西。
windows下編譯Libevent