thrift的使用

yum install automake libtool flex bison pkgconfig gcc-c++ boost-devel libevent-devel zlib-devel python-devel ruby-devel  

wget http://archive.apache.org/dist/thrift/0.9.0/thrift-0.9.0.tar.gz  
tar zxvf thrift-0.9.0  
cd thrift-0.9.0  

/usr/local/include/thrift/transport/TSocket.h:216:3: 错误:‘sockaddr’不是一个类型名

在TSocket.h中加入

#include<sys/socket.h>
#include<arpa/inet.h>



 /usr/local/include/thrift/protocol/TProtocol.h:646:11: error: 'uint32_t' does not name a type

All you need to do is add the following line before include thrift.h in the header file you cannot compile:

       #include <stdint.h>


‘htons’的声明必须可用[-fpermissive],则修改Makefile,加上编译选项-DHAVE_NETINET_IN_H
DHAVE_NETINET_IN_H 这个编译选项要有


thrift -r --gen py:twisted example.thrift  产生异步的py server
thrift -r --gen cpp:cob_style example.thrift 产生异步的cpp server

thrift使用时候的注意事项:
  1. 如果服务器采用TNonblockingServer的话,客户端必须采用TFramedTransport。程序链接的时候需要thriftnb。
  2. 使用ThreadServer和SimpleServer的时候,可以使用Framed或者是Http的
  3. 现在客户端好像只支持Http协议的异步通信,没有找到Framed方式的通信方式 (C++客户端异步支持不好,或者至少这方面的信息太少,让人了解困难 暂只略知有个基于http的异步支持,java客户端的异步支持感觉不错)
  4. thrift目前只支持python2.6+,但不支持3.XX版本

如果报错:

src/thrift/qt/moc_TQTcpServer.cpp:14:2: error: #error "This file was generated using the moc from 4.8.1. It"
src/thrift/qt/moc_TQTcpServer.cpp:15:2: error: #error "cannot be used with the include files from this version of Qt."
src/thrift/qt/moc_TQTcpServer.cpp:16:2: error: #error "(The moc has changed too much.)"

则运行下面命令:

#mv thrift-0.9.0/lib/cpp/src/thrift/qt/moc_TQTcpServer.cpp  thrift-0.9.0/lib/cpp/src/thrift/qt/moc_TQTcpServer.cpp.bak

安装thrift之前,可能需要先安装libevent,否则生成不了thriftnb,因为这个需要libevent支持
原文地址:https://www.cnblogs.com/bornfish/p/4184099.html