win7环境下VS2013编译boost_1_58_0步骤:
- 官网下载boost_1_58_0(直接下载),解压
- cmd窗口cd到boost_1_58_0,执行bootstrap.bat
- cmd窗口获取动态库命令:bjam install stage --toolset=msvc-12.0 -- stagedir="C:\Boost\boost_vc_120" link=shared runtime-link=shared threading=multi debug release cmd窗口获取静态库命令:bjam install stage --toolset=msvc-12.0 -- stagedir="C:\Boost\boost_vc_120" link=static runtime-link=static threading=multi debug release
- cmd窗口运行:b2 install
- 完毕后会在C:\Boost目录下生成include和lib两个目录,其中lib目录和include目录下boost-1_58这两个目录为编译成功后得到的两个目录
- 新建Win32 Console Application项目,Console application+Empty project,去掉Precomplied header前面的勾。
- 工程上右键Properties,C++里的General,Additional Include Directiories添加boost-1_58目录
- Linker里的General,Additional Library Directories添加lib目录
- 添加source.cpp,代码如下,编译,如果成功代表boost编译成功
1 #include <iostream> 2 #include <boost/thread/thread.hpp> 3 4 void hello() 5 { 6 std::cout << "Hello world, I'm a thread!" << std::endl; 7 } 8 int main() 9 { 10 boost::thread thrd(&hello); 11 thrd.join(); 12 }
win7环境下VS2013编译libevent-2.0.22-stable步骤:
- 官网下载libevent-2.0.22-stable(使用git)
- 修改以下三个文件libevent-2.0.22-stable\event_iocp.c,libevent-2.0.22-stable\evthread_win32.c,libevent-2.0.22-stable\listener.c,在这三个文件开头添加宏定义#define _WIN32_WINNT 0x0500
- Microsoft visual studio 2013命令行工具,cd到levent-libevent目录,执行nmake /f Makefile.nmake
- 编译成功后,在当前目录会生成三个lib,分别为:libevent_core.lib,libevent_extras.lib和libevent.lib
- 新建一个lib目录,将这三个lib文件拷贝到此目录;新建一个include目录,将libevent-2.0.22-stable\include目录下的文件和文件夹拷贝到此目录下,将libevent-2.0.22-stable\WIN32-Code下的文件和libevent-2.0.22-stable\WIN32-Code\nmake下的目录和文件拷贝到此目录下,2个event2目录下的文件直接合并即可
- 新建Win32 Console Application项目,Console application+Empty project,去掉Precomplied header前面的勾。
- 工程上右键Properties,VC++ Directories里的Include Directories添加include目录,Library Directories添加lib目录
- C++里的Code Generation,Runtime Library选择Multi-threaded Debug (/MTd)(Debug下),Multi-threaded (/MT)(Release下)
- Linker里的Input,Additional Dependencies添加ws2_32.lib;wsock32.lib;libevent.lib;libevent_core.lib;libevent_extras.lib;
- 添加source.cpp,代码如下,启动项目,打开cmd输入(需要在控制面板\打开windows功能中勾选telnet客户端):telnet 127.0.0.1 55555,效果图如下:
source.cpp源码如下:
1 #include <string.h> 2 #include <errno.h> 3 #include <stdio.h> 4 #include <signal.h> 5 6 #ifndef WIN32 7 #include <netinet/in.h> 8 # ifdef _XOPEN_SOURCE_EXTENDED 9 # include <arpa/inet.h> 10 # endif 11 #include <sys/socket.h> 12 #endif 13 14 #include "event2/bufferevent.h" 15 #include "event2/buffer.h" 16 #include "event2/listener.h" 17 #include "event2/util.h" 18 #include "event2/event.h" 19 20 #include <WinSock2.h> 21 22 static const char MESSAGE[] = "Hello, World!\n"; 23 24 static const int PORT = 9995; 25 26 27 static void conn_writecb(struct bufferevent *bev, void *user_data) 28 { 29 struct evbuffer *output = bufferevent_get_output(bev); 30 if (evbuffer_get_length(output) == 0) 31 { 32 printf("flushed answer\n"); 33 bufferevent_free(bev); 34 } 35 } 36 37 static void conn_eventcb(struct bufferevent *bev, short events, void *user_data) 38 { 39 if (events & BEV_EVENT_EOF) 40 { 41 printf("Connection closed.\n"); 42 } 43 else if (events & BEV_EVENT_ERROR) 44 { 45 printf("Got an error on the connection: %s\n", 46 strerror(errno));/*XXX win32*/ 47 } 48 /* None of the other events can happen here, since we haven't enabled 49 * timeouts */ 50 bufferevent_free(bev); 51 } 52 53 static void signal_cb(evutil_socket_t sig, short events, void *user_data) 54 { 55 struct event_base *base = (struct event_base *)user_data; 56 struct timeval delay = { 2, 0 }; 57 58 printf("Caught an interrupt signal; exiting cleanly in two seconds.\n"); 59 60 event_base_loopexit(base, &delay); 61 } 62 63 static void listener_cb(struct evconnlistener *listener, evutil_socket_t fd, 64 struct sockaddr *sa, int socklen, void *user_data) 65 { 66 struct event_base *base = (struct event_base *)user_data; 67 struct bufferevent *bev; 68 69 bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE); 70 if (!bev) 71 { 72 fprintf(stderr, "Error constructing bufferevent!"); 73 event_base_loopbreak(base); 74 return; 75 } 76 bufferevent_setcb(bev, NULL, conn_writecb, conn_eventcb, NULL); 77 bufferevent_enable(bev, EV_WRITE); 78 bufferevent_disable(bev, EV_READ); 79 80 bufferevent_write(bev, MESSAGE, strlen(MESSAGE)); 81 } 82 83 int main(int argc, char **argv) 84 { 85 struct event_base *base; 86 struct evconnlistener *listener; 87 struct event *signal_event; 88 89 struct sockaddr_in sin; 90 91 #ifdef WIN32 92 WSADATA wsa_data; 93 WSAStartup(0x0201, &wsa_data); 94 #endif 95 96 base = event_base_new(); 97 if (!base) 98 { 99 fprintf(stderr, "Could not initialize libevent!\n"); 100 return 1; 101 } 102 103 memset(&sin, 0, sizeof(sin)); 104 sin.sin_family = AF_INET; 105 sin.sin_port = htons(PORT); 106 107 listener = evconnlistener_new_bind(base, listener_cb, (void *)base, 108 LEV_OPT_REUSEABLE|LEV_OPT_CLOSE_ON_FREE, -1, 109 (struct sockaddr*)&sin, 110 sizeof(sin)); 111 112 if (!listener) 113 { 114 fprintf(stderr, "Could not create a listener!\n"); 115 return 1; 116 } 117 118 signal_event = evsignal_new(base, SIGINT, signal_cb, (void *)base); 119 120 if (!signal_event || event_add(signal_event, NULL)<0) 121 { 122 fprintf(stderr, "Could not create/add a signal event!\n"); 123 return 1; 124 } 125 126 event_base_dispatch(base); 127 128 evconnlistener_free(listener); 129 event_free(signal_event); 130 event_base_free(base); 131 132 printf("done\n"); 133 return 0; 134 }
win7环境下VS2013编译openssl步骤:
- 下载安装ActivePerl,官网下载openssl源码(使用git,若在官网上下载压缩包解压会出现错误<不知道为什么?>)
- Microsoft visual studio 2013命令行工具,cd到openssl目录
- 执行命令:perl Configure VC-WIN32 no-asm --prefix=c:\openssl_lib
- 执行命令:ms\do_ms
- 执行命令:nmake -f ms\ntdll.mak (编译动态库) nmake -f ms\nt.mak (编译静态库)
- 执行命令:nmake -f ms\ntdll.mak install (装载动态库) nmake -f ms\nt.mak install (装载静态库)
- 执行命令:nmake -f ms\ntdll.mak test (测试动态库) nmake -f ms\nt.mak test (测试静态库)
- openssl目录下的include和lib即为编译成功后得到的两个目录
- 新建Win32 Console Application项目,Console application+Empty project,去掉Precomplied header前面的勾。
- 工程上右键Properties,C++里的General,Additional Include Directiories添加include目录
- Linker里的General,Additional Library Directories添加lib目录
- 添加source.cpp,代码如下,启动项目
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 #include <openssl/bn.h> 6 #include <openssl/ec.h> 7 #include <openssl/rand.h> 8 #include <openssl/err.h> 9 #include <openssl/ecdsa.h> 10 #include <openssl/ecdh.h> 11 12 #pragma comment(lib, "libeay32.lib") 13 14 int main() 15 { 16 17 return 0; 18 }
win7环境下VS2013编译thrift步骤:
- 下载thrift-0.9.2(本人使用git下载源码包)
- 打开thrift-0.9.2\lib\cpp下的sln项目,按照上文所述步骤依次为两个项目(libthrift和libthriftnb,两种模式)添加boost、libevent和openssl依赖项,编译Solution即可得到这两种模式下对应的lib文件(分别为libthrift.lib和libthriftnb.lib)和一个src目录。(注意debug版本和release版本需要分别添加,在solution上右键Properties的Configuration上改变编译方式,分别编译可得到这两种版本对于的四个lib文件)
初试thrift:
- 下载thrift-0.9.2.exe,更名为thrift.exe,准备运行源码包里的cpp例程(在thrift-0.9.2\tutorial目录下,包括shared.thrift、tutorial.thrift和thrift-0.9.2\tutorial\cpp目录下的CppClient.cpp和CppServer.cpp)
- 新建目录ThriftCreateCode,将thrift.exe放入,将shared.thrift和tutorial.thrift这两个文件拷贝到当前目录,cmd窗口cd到ThriftCreateCode,依次执行命令:thrift --gen cpp shared.thrift和thrift --gen cpp tutorial.thrift(这两个命令部分先后),将会在当前目录下产生gen-cpp,此目录下将会产生多个.h和.cpp文件,如下图(其中两个_server.skeleton.cpp为给的示例server代码,供参考):
- 新建Solution,取名为ThriftCPPTest,并新建两个Win32 Console Application项目,Console application+Empty project,去掉Precomplied header前面的勾,分别为Client和Server。
- 分别为Client和Server添加thrift依赖项:工程上右键Properties,VC++ Directories里的Include Directories添加src目录,Library Directories添加lib目录(包含debug或者release里的两个lib文件);C++里的Code Generation,Runtime Library选择Multi-threaded Debug (/MTd)(Debug下),Multi-threaded (/MT)(Release下);Linker里的Input,Additional Dependencies添加libthrift.lib;libthriftnb.lib;
- 分别为Client和Server添加Boost依赖项
- 分别为Client项目和Server项目添加相对应的.h和.cpp文件,添加的文件和运行效果见下图:
最后我想说一句,对这些开源项目,真特么是操蛋,网上资料很杂,一种一种的试,我真是醉了。
本文参考资料:
win7环境下VS2013编译boost_1_58_0:http://jingyan.baidu.com/article/a3aad71aa1ebe7b1fb009681.html
win7环境下VS2013编译libevent-2.0.22-stable步骤:http://www.cnblogs.com/luxiaoxun/p/3603399.html
http://zengwu3915.blog.163.com/blog/static/2783489720153402449305/
win7环境下VS2013编译openssl:http://blog.csdn.net/liuhongxiangm/article/details/18400837
http://blog.sina.com.cn/s/blog_55feec4101010x51.html
thrift初试:http://blog.csdn.net/hbuxiaoshe/article/details/6558391