thrift-0.9.0windows下编译

本文介绍了如何在Windows 6.1 SP1(Win7旗舰版)上,使用VS2010 SP1和Cygwin编译Thrift 0.9.0。详细步骤包括安装所需依赖、修改源代码、配置和编译Thrift,以及创建和测试Thrift服务器和客户端应用。同时提供了针对Windows环境的源代码修改示例。
摘要由CSDN通过智能技术生成

windows 版本6.1 sp1 win7旗舰版
VS2010 SP1
下载安装cygwin
http://thrift.apache.org/docs/install/
make/g++ 4.2+/gcc/boost 1.40.0+/lex/yacc/flex/autoconf/automake/libtool/zlib/看还需要啥搜索啥
thrift 版本:0.9.0 http://thrift.apache.org/download/
代码修改:
\thrift-0.9.0\lib\cpp\src\thrift\concurrency\Mutex.cpp 246行 Mutex::RECURSIVE_INITIALIZER:
void Mutex::RECURSIVE_INITIALIZER(void* arg) {
  //init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE_NP);
  // modified by uniqs 20140217
  init_with_kind((pthread_mutex_t*)arg, PTHREAD_MUTEX_RECURSIVE);
}

\thrift-0.9.0\lib\cpp\src\thrift\concurrency\ThreadManager.h  24 行 :
// modified by uniqs 20140217
#include <boost/tr1/functional.hpp>
//#include <tr1/functional>

\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 154行 TServerSocket::listen:
 void TServerSocket::listen() {
  // modified by uniqs 20140217
#ifdef _WIN32
  WSADATA kData;
  WSAStartup(MAKEWORD(2, 2), &kData);
#endif

\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 175行 TServerSocket::listen:
  // modified by uniqs 20140217
#ifdef _WIN32
  hints.ai_family = PF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = 0;
#else
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
#endif
   //   hints.ai_family = PF_UNSPEC;
   //   hints.ai_socktype = SOCK_STREAM;
   //   hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;

\thrift-0.9.0\lib\cpp\src\thrift\transport\TServerSocket.cpp 206行 TServerSocket::listen:
  if (! path_.empty()) {
   // modified by uniqs 20140217
#ifdef _WIN32
   serverSocket_ = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
#else
   serverSocket_ = socket(PF_UNIX, SOCK_STREAM, IPPROTO_IP);
#endif
  } else {
   serverSocket_ = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
  }
  
编译:
http://thrift.apache.org/docs/BuildingFromSource/
http://thrift.apache.org/docs/install/windows/
cd thrift-0.9.0
./configure
make
make install
测试是否成功:thrift -version
VS2010新建工程,thrift_cpp_server 同时新建同名项目,引入\thrift-0.9.0\lib\cpp中的libthrift
定义文件 User.thrift :
struct User {

1: i32 id,

2: string name,

}

service UserStorage {

void set_user(1: string name)

User get_user(1: i32 id)

}

thrift -r --gen cpp User.thrift
在thrift_cpp_server项目中导入所有生成的文件,并修改 UserStorage_server.skeleton.cpp 为 UserStorage_server.cpp
简单修改如下:
class UserStorageHandler : virtual public UserStorageIf {
private:
 typedef map<int, User> ARRUSERS;
 ARRUSERS m_mapUsers;
 static unsigned int m_uUserIdIndex;
 ......
 ~UserStorageHandler()
 {
  m_mapUsers.clear();
 }
 
 void set_user(const std::string& name) {
  // Your implementation goes here
  User kUser;
  kUser.__set_name(name);
  kUser.__set_id(++m_uUserIdIndex);
  m_mapUsers[kUser.id] = kUser;
  printf("set_user\n");
 }

 void get_user(User& _return, const int32_t id) {
  // Your implementation goes here
  ARRUSERS::iterator it = m_mapUsers.find(id);
  if (it != m_mapUsers.end())
  {
   _return = it->second;
  }

  printf("get_user\n");
 }
 ......

unsigned int UserStorageHandler::m_uUserIdIndex = 0;
编译链接

thrift --gen csharp User.thrift
再新建一个csharp工程test_csharp_client,注意.netframework的版本一定要与后导入的Thrift项目的.netframework版本一致。
新建项目thrift_csharp_client。并导入 \thrift-0.9.0\lib\csharp\src 中的 Thrift 项目
在thrift_csharp_client的自动生成的Program.cs中加入如下代码 :
......
using Thrift;
using Thrift.Transport;
using Thrift.Protocol;
......
        static void Main(string[] args)
        {
            TTransport transport = new TSocket("localhost", 9090);
            TProtocol protocol = new TBinaryProtocol(transport);
            UserStorage.Client client = new UserStorage.Client(protocol);

            transport.Open();

            client.set_user("aaaaa");
            User u = client.get_user(1);
            Console.WriteLine(u.Name);
            transport.Flush();
            transport.Close();
......
在工程中添加Program.cs/UserStorage.cs文件,添加Thrift项目的引用

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值