thrift编译方法

Thrift可以实现C++、Java、Python等多种语言的自动生成,此处以C++为例。


本文共5部分,其中1,2和3部分为重点。


1. 编写[.thrift]文件

你可以google到官方的实例如下:

  1. structStudent{
  2. 1:i32sno,
  3. 2:stringsname,
  4. 3:boolssex,
  5. 4:i16sage,
  6. }
  7. serviceServ{
  8. voidput(1:Students),
  9. }
struct Student{
 1: i32 sno,
 2: string sname,
 3: bool ssex,
 4: i16 sage,
}
service Serv{
 void put(1: Student s),
}

将上述代码保存为student.thrift文件。


2. 自动生成服务器端程序

在Terminal中输入如下命令,可自动生成[.cpp]和[.h]文件。

  1. thrift-r--gencppstudent.thrift
thrift -r --gen cpp student.thrift

得到的文件如下:

  1. Serv.cpp
  2. Serv.h
  3. Serv_server.skeleton.cpp
  4. student_constants.cpp
  5. student_constants.h
  6. student_types.cpp
  7. student_types.h
Serv.cpp
Serv.h
Serv_server.skeleton.cpp
student_constants.cpp
student_constants.h
student_types.cpp
student_types.h

其中Serv_server.skeleton.cpp中有服务器端运行的main函数。这些文件名的Serv和student与你最初创建的thrift文件有关。


3. 编写客户端程序

  1. #include"Serv.h"//Your.hFile
  2. #include<transport/TSocket.h>
  3. #include<transport/TBufferTransports.h>
  4. #include<protocol/TBinaryProtocol.h>
  5. usingnamespaceapache::thrift;
  6. usingnamespaceapache::thrift::protocol;
  7. usingnamespaceapache::thrift::transport;
  8. usingboost::shared_ptr;
  9. intmain(intargc,char**argv){
  10. boost::shared_ptr<TSocket>socket(newTSocket("localhost",9090));
  11. boost::shared_ptr<TTransport>transport(newTBufferedTransport(socket));
  12. boost::shared_ptr<TProtocol>protocol(newTBinaryProtocol(transport));
  13. transport->open();
  14. //YourCodes
  15. transport->close();
  16. return0;
  17. }
#include "Serv.h"  // Your .h File
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>

using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;

using boost::shared_ptr;

int main(int argc, char **argv) {
	boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
	boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
	boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

	transport->open();

	// Your Codes

	transport->close();

	return 0;
}

4. 编译/链接

  1. g++-g-I/home/michael/opt/include/thrift-L/home/michael/opt/lib/-lthriftServ.cppstudent_types.cppstudent_constants.cppServ_server.skeleton.cpp-oserver
  2. g++-g-I/home/michael/opt/include/thrift-L/home/michael/opt/lib/-lthrift-lm-pthread-lz-lrt-lsslServ.cppstudent_types.cppstudent_constants.cppclient.cpp-oclient
g++ -g -I/home/michael/opt/include/thrift -L/home/michael/opt/lib/ -lthrift Serv.cpp student_types.cpp student_constants.cpp Serv_server.skeleton.cpp -o server

g++ -g -I/home/michael/opt/include/thrift -L/home/michael/opt/lib/ -lthrift -lm -pthread -lz -lrt -lssl Serv.cpp student_types.cpp student_constants.cpp client.cpp -o client

5. 运行

  1. ./server
  2. ./client
./server

./client


评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值