前言:
  thrift是出于Facebook的rpc网络编程框架, 其对跨平台和多语言的支持优于google protobuf, 但thrift在java/c#语言上应用比较多, 资料也丰富, 在windows平台的c++这块, 资料相对较少, 而且编译也麻烦. 这篇博客主要记录对thrift在windows上的编译和使用过程, 不涉及原理, 也不具体涉及应用.如有不足, 请各位指正.

执行过程
1. 下载并安装Visual Studio
notice: visual studio 有windows版本限制, 比如visual studio 2013在windows 7就安装不了
参考网址: http://www.visualstudio.com/zh-cn/visual-studio-2013-compatibility-vs
系统: windows7 + visual studio 2012

2. boost安装/编译/链接
  具体步骤如下:
  *) 下载boost
    1. 下载 boost_1_55_0.zip
  *) 编译boost
    1. 执行 bootstrap.bat
    2. 执行 b2.exe (编译的时间较长, 请耐心等待)
  *) 验证boost
    在virtual studio的window console工程属性中添加如下:
    1. 附加包含目录: $BOOST_HOME
    2. 附加库目录: $BOOST_HOME\stage\lib
    3. 编写如下代码进行编译/运行认证

 #include <iostream>
#include <string>
#include <boost/regex.hpp>
int main()
{
boost::regex pattern("\\w+@\\w+(\\.\\w+)*");
std::string mail("xxx@gmail.com"); if ( boost::regex_match(mail, pattern) ) {
std::cout << mail << " is a valid mail address!" << std::endl;
} else {
std::cout << mail << " is not a valid mail address!" << std::endl;
}
}

  安装boost和配置visual studio的参考网址如下所示:
  http://blog.csdn.net/stanfordzhang/article/details/8587282
  http://www.cnblogs.com/me115/archive/2010/10/08/1845825.html
  http://www.cnblogs.com/chuncn/archive/2012/09/10/2679026.html

3. libevent的编译/安装/链接
  *) 参考的编译/安装过程网页
  http://blog.s135.com/libevent_windows/
  *) 下载libevent
  http://libevent.org/
  *) 编译libevent
  遇到的编译错误处理方案
  http://10305101ivy.blog.163.com/blog/static/584765892012227322607/

  http://blog.csdn.net/boyxiaolong/article/details/17057063
  evutil.c添加如下行:

 #ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#include <io.h>
#include <tchar.h>
#endif

  nmake /f Makefile.nmake
  生成libevent_core.lib libevent_extras.lib libevent.lib
  若遇到头文件找不到的问题, 需要手动修改Makefile.nmake文件, 添加相关的头文件路径

CFLAGS=/IWIN32-Code /Iinclude /Icompat /DWIN32 /DHAVE_CONFIG_H /I. /I"C:\Program Files (x86)\Windows Kits\8.0\Include\um" /I"C:\Program Files (x86)\Windows Kits\8.0\Include\shared" /I"C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include"
    具体添加的原则是编译缺那个头文件, 就去添加相关的系统头文件目录   
    编写libevent代码编译/运行成功
 #include <event.h>
#include <stdio.h> int main()
{
const char *version = event_get_version();
printf("%s\n",version);
return ;
}
    附加依赖项: ws2_32.lib , libevent_core.lib ,libevent.lib, libevent_extras.lib

4. thrift的编译/链接
  *)下载thrift 0.9.0源码
  下载网址: http://archive.apache.org/dist/thrift
  *)thrift依赖的库
  http://www.coder4.com/archives/3777
  thrift 依赖 boost库(1.4.7), thriftnb 依赖 boost/libevent库
  http://www.iteye.com/problems/87958
  thrift在编译过程中, 会遇到二义性
  “_wassert”: 对重载函数的调用不明确
  void _wassert(const wchar_t *,const wchar_t *,unsigned int)
  void apache::thrift::protocol::_wassert(const wchar_t *,const wchar_t *,unsigned int)
  解决方案:
  这算命令空间污染的问题, 添加::, 使得对_wassert的调用采用全局声明的那个函数

 assert.h

 #define assert(_Expression) (void)( (!!(_Expression)) || (_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )

 #define assert(_Expression) (void)( (!!(_Expression)) || (::_wassert(_CRT_WIDE(#_Expression), _CRT_WIDE(__FILE__), __LINE__), 0) )
    
    测试验证:
    编写 hello.thrift 文件
 namespace cpp test

 service HelloService {
string hello(: string username);
}
  thrift.exe -gen cpp hello.thrift 
 // : , HelloService.cpp
// : , HelloService.h
// : , HelloService_server.skeleton.cpp
// : hello_constants.cpp
// : hello_constants.h
// : hello_types.cpp
// : hello_types.h 
    编译执行, 遇到10093错误, 如何去解决?
    WSANOTINITIALISED,  which means WSAStartup() has not been called yet.
    编译完成后运行时会报WSAStartup错误    
    解决方案:
    http://hi.baidu.com/fsx92/item/9f7a96efd33f9f1b585dd88c                  
    编写测试case
  服务端代码:
 class HelloServiceHandler : virtual public HelloServiceIf {
public:
HelloServiceHandler() {
// Your initialization goes here
} void hello(std::string& _return, const std::string& username) {
_return = "hello " + username;
} }; int main(int argc, char **argv) {
int port = ; TWinsockSingleton::create(); // 需要用户自己添加, 进行WSAStartup的初始化, 算是windows 版的thrift的一个疏忽 shared_ptr<HelloServiceHandler> handler(new HelloServiceHandler());
shared_ptr<TProcessor> processor(new HelloServiceProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory()); TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return ;
}
    客户端代码 
 #include "HelloService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TSocket.h> using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server; using boost::shared_ptr; using namespace ::test; int main()
{ shared_ptr<TTransport> socket(new TSocket("127.0.0.1", ));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport)); HelloServiceClient client(protocol); try {
transport->open();
std::string res;
client.hello(res, "lilei");
std::cout << res << std::endl;
} catch(TException &e) {
std::cout << e.what() << std::endl;
} return ;
}
    推荐做法:
    对依赖库的整理, 这是一个好的习惯
    每个库单独创建一个头文件目录, 和库文件目录, 所有的库统一在同一个库仓库下,  c++的库管理不如java的maven那么方便, 又进入一个石器时代, 库的维护需要开发者手动去支持, 但这是种很好的工程实践.     
    
    repository目录为顶级的仓库目录, 以boost为例, boost表示库名, 之下boost/1.55.0为boost的具体的某一版本, 而boost/0.55.0/include为这个版本的头文件目录, boost/0.55.0/lib为这个版本的lib库目录

thrift在windows的编译/安装--c++版的更多相关文章

  1. RPC与实践(thrift在windows的编译/安装--c++版)

    ------------------------------------------------------------------------ RPC 功能目标 RPC 的主要功能目标是让构建分布式 ...

  2. Apache Thrift 在Windows下的安装与开发

    Windows下安装Thrift框架的教程很多.本文的不同之处在于,不借助Cygwin或者MinGW,只用VS2010,和Thrift官网下载的源文件,安装Thrift并使用. 先从官网 下载这两个文 ...

  3. N2N windows下编译安装文件

    n2n安装 n2n原理编译版下载,可直接使用:windows下vpn客户端 n2n_v2_linux_x64 n2n_v2_Win32TAP网卡驱动 #linux环境编译yum install -y ...

  4. Windows下编译安装 FFmpeg

    在Linux/Mac下编译 ffmpeg是非常方便的.但要在 Windows下编译 ffmpeg还真要花点时间.以下就是在 Windowns下编译ffmpeg的步骤: 一.安装Cygwin 在wind ...

  5. ubuntu14.04 编译安装CPU版caffe

      本文,试图中一个干净的ubuntu14.04机器上安装caffe的cpu版本. http://blog.csdn.net/sinat_35188997/article/details/735304 ...

  6. windows下编译安装BOOST

    boost的编译和使用,经过搜集资料和总结,记录成文.感谢文后所列参考资料的作者. 1 下载 地址:http://sourceforge.net/projects/boost/files/boost/ ...

  7. ubuntu thrift 0.9.3编译安装

    Table of Contents 1. 下载thrift源代码 2. 编译并安装 3. 运行测试程序 4. 安装 1 下载thrift源代码 git clone https://git-wip-us ...

  8. Windows Server 2016 安装虚拟机版黑群晖

    硬件配置 Dell R730 CPU: Intel(R) Xeon(R) CPU E5-2603 v4 @1.70GHz(6 cores) Ram: 16Gb HDD: 系统-600GB SAS X2 ...

  9. windows VS2013 编译安装QWT6.1和QWTPolar1.1.1

    QWT的编译和配置 1. 下载QWT从官网 For getting a snapshot with all bugfixes for the latest 5.2 release: svn expor ...

随机推荐

  1. 作用域闭包、预解释和this关键字综合题目

    var number = 2; var obj = {number : 5, fn1 : ( function() { this.number *= 2; number=number*2; var n ...

  2. L1 - 闭包和原型链

    先来一炮尝尝: var i = 10; function myFunc(){ var i = 20; function innerFunc(){ alert(i); } return innerFun ...

  3. HDU 4050 wolf5x 概率dp 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=4050 题意: 现在主角站在0处,需要到达大于n的位置 主角要进入的格子有三种状态: 0. 不能进入 1. 能进入 ...

  4. POJ 1719 二分图最大匹配(记录路径)

    Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4097   Accepted: 1499 ...

  5. Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)

    Java中实现多线程的方法有两种: 继承Thread类和实现Runnable方法,并重写Run方法,然后调用start()方法启动线程.使用Runnable会比Thread要好很多,主要是以下三个原因 ...

  6. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  7. how to reset mac root password

    Reset 10.5 Leopard & 10.6 Snow Leopard password Power on or restart your Mac. At the chime (or g ...

  8. 支持多人协作的在线免费作图工具:ProcessOn

    之前朋友给我推荐一款作图工具ProcessOn,出于好奇我就研究了一下它,今天我就给大家简单介绍一下这款免费的在线作图工具:ProcessOn 首先使用ProcessOn我们需要有一个帐号,这样每次操 ...

  9. @ModelAttribute注解的作用

    @ModelAttribute注解的作用:1.放在方法上注解不带属性: 方法无返回值: 执行其他方法时,先执行该注解标记方法. 如果方法中有将一些属性放入model的操作,其他方法model中也会共享 ...

  10. 2013年9月份第1周51Aspx源码发布详情

    大型B2B家具门户网源码  2013-9-6 [VS2008]功能描述: 1.门户信息管理 安全取数据即使数据库连接中断不会报错 2.稳定性 每句代码经过3次以上检查.此网站还在运营3年了,没有出过问 ...