Win7 上使用 VS2015 编译使用 Thrift
一.编译 Boost
1:从官方网站(http://www.boost.org/)上下载某一版本的 Boost,比如我下载的 boost_1_68_0.tar.gz 的版本.
2:将下载好的 boost_1_68_0.tar.gz 解压到某一路径下(比如:E:\boost_1_68_0)。
3:打开 CMD 窗口,cd 到 boost 解压的目录下。运行 bootstrap.bat 批处理文件。
4:完成 3 后,当前目录下会生成一个 bjam.exe 文件和 b2.exe 文件。直接执行其中任意一个 exe 文件(我执行
的是 bjam.exe),会进行大约 15 分钟的库的编译(生成动态链接库和静态链接库)。
注意:在编译前,需要安装某一个版本的 Visual Studio。我安装的是 Visual Studio 2015
二.编译 openssl
参考:https://blog.csdn.net/zhuyinglong2010/article/details/80781556
三.编译 libEvent
参考:https://blog.csdn.net/tpriwwq/article/details/53467128
可以只包括其头文件,可以不用编译
四.编译 Thrift(libthrift)
1. 从官网上载安装包 https://thrift.apache.org/download ;
2. 解压后使用 VS2015 打开 thrift-master\lib\cpp 的 sln 文件(1 个阻塞的 libthrift,另一个是非阻塞的
libthriftnb);
3. 在工程属性中“VC++目录”中的“包含目录”包括 boost、openssl 和 libevent 的代码目录(例如包含
E:\boost_1_68_0)。
4. 在工程属性中“VC++目录”中的“库目录”包括 boost 和 openssl 的 lib 库。
5. 编译成功则出现 libthrift.lib.
五. 使用 thrift 例子
1. 参考:https://blog.csdn.net/feng973/article/details/70160571
2. 遇到的问题:
A. 在工程属性中“VC++目录”中的“包含目录”中需要包括 boost、openssl 、libevent 和 thrift 的代
码目录;
B. 在工程属性中“链接器”中的“输入”中“附加依赖项”至少包含 libthrift.lib、libcrypto.lib 和 libssl.lib,
libcrypto.lib 和 libssl.lib 是 openssl 库;
C.无法使用 boost::shared_ptr,只好使用 std:: shared_ptr;
D. PlatformThreadFactory 替换 PosixThreadFactory;
shared_ptr
handler(new ServHandler());
shared_ptr processor(new ServProcessor(handler));
shared_ptr protocolFactory(new TBinaryProtocolFactory());
shared_ptr transportFactory(new TBufferedTransportFactory());
shared_ptr serverTransport(new TServerSocket(9090));
// 指定15个线程
shared_ptr threadManager = ThreadManager::newSimpleThreadManager(2);
shared_ptr threadFactory
= shared_ptr(new PlatformThreadFactory());
threadManager->threadFactory(threadFactory);
threadManager->start();
printf("start.../n");
TThreadPoolServer server(processor,
serverTransport,
transportFactory,
protocolFactory,
threadManager);
server.serve();