ubuntu下安装使用Thrift



1、简介

Thrift是Facebook的一个开源项目,主要是一个跨语言的服务开发框架。它有一个代码生成器来对它所定义的IDL定义文件自动生成服务代 码框 架。用户只要在其之前进行二次开发就行,对于底层的RPC通讯等都是透明的。目前它支持的语言有C++, Java, Python, PHP,   Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

环境:
OS:Ubuntu 12.04.1 LTS
内核:3.2.0-33-generic-pae


2、下载thrift

1
wget http: //labs .renren.com /apache-mirror//thrift/0 .9.0 /thrift-0 .9.0. tar .gz(下载版本为0.9.0)

3、安装依赖包

1
sudo  apt-get  install  libboost-dev libboost- test -dev  libboost-program-options-dev libevent-dev automake libtool flex bison  pkg-config g++ libssl-dev ant

4、解压

1
tar  -zxvf thrift-*. tar .gz

进入目录cd thrift-*

5、安装编译

1
2
3
. /configure
make
sudo  make  install

6、安装测试
安装后thrift -version查看版本信息,表示安装成功。
Thrift version 0.9.0


7、自带例子测试

thrift中有自带的例子,在tutorial目录内,我们测试其中cpp的例子。

进入tutorial目录,shared.thrift和tutorial.thrift是接口定义文件。

执行 thirft -r --gen cpp tutorial.thrift,可以生成gen-cpp目录,这是thrift编译器自动生成的代码。

然后进入tutorial/cpp目录,执行make,生成可执行文件。不过make之后会有各式各样的错误,我们一一改正。


8、make之后可能出现的错误及改正

(1) 错误1:非常多的错误,找到第一个错误是

大意是/usr/local/include/thrift/concurrency/Thread.h中的pthread_self、pthread_equal等没有声明,搜了一下好像很少有人遇到过这个问题。不过不打紧,错误的意思可以理解,搜到pthread_self、pthread_equal对应的头文件是#include <pthread.h>,加到那个Thread.h中就好了。


(2)错误2:/usr/local/include/thrift/protocol/TBinaryProtocol.tcc:147:35: 错误:htons’的实参不依赖模板参数,

解决:‘htons’的声明必须可用[-fpermissive],则修改Makefile,加上编译选项-DHAVE_NETINET_IN_H

修改后的Makefile(部分)

1
2
3
4
server: CppServer.cpp
g++ -DHAVE_NETINET_IN_H -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR} -I.. /gen-cpp  -L${LIB_DIR} -lthrift CppServer.cpp ${GEN_SRC}
client: CppClient.cpp
g++ -DHAVE_NETINET_IN_H -o CppClient -I${THRIFT_DIR} -I${BOOST_DIR} -I.. /gen-cpp  -L${LIB_DIR} -lthrift CppClient.cpp ${GEN_SRC}


(3)错误3:仍然是非常多的错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
g++ -DHAVE_NETINET_IN_H -o CppServer -I /usr/local/include/thrift  -I /usr/local/boost/include/boost-1_33_1/   -I.. /gen-cpp  -L /usr/local/lib  -lthrift CppServer.cpp .. /gen-cpp/SharedService .cpp .. /gen-cpp/shared_types .cpp .. /gen-cpp/tutorial_types .cpp .. /gen-cpp/Calculator .cpp
/tmp/cc3gKFHw .o: In  function  `main':
CppServer.cpp:(.text+0xd1): undefined reference to `apache::thrift::transport::TServerSocket::TServerSocket(int)'
CppServer.cpp:(.text+0x15f): undefined reference to `apache::thrift::server::TSimpleServer::serve()'
/tmp/cc3gKFHw .o: In  function  `apache::thrift::server::TSimpleServer::~TSimpleServer()':
CppServer.cpp:(.text._ZN6apache6thrift6server13TSimpleServerD2Ev[_ZN6apache6thrift6server13TSimpleServerD5Ev]+0xb): undefined reference to `vtable  for  apache::thrift::server::TSimpleServer'
/tmp/cc3gKFHw .o: In  function  `apache::thrift::transport::TBufferedTransport::TBufferedTransport(boost::shared_ptr<apache::thrift::transport::TTransport>)':
CppServer.cpp:(.text._ZN6apache6thrift9transport18TBufferedTransportC2EN5boost10shared_ptrINS1_10TTransportEEE[_ZN6apache6thrift9transport18TBufferedTransportC5EN5boost10shared_ptrINS1_10TTransportEEE]+0x17): undefined reference to `vtable  for  apache::thrift::transport::TBufferedTransport'
/tmp/cc3gKFHw .o: In  function  `apache::thrift::server::TSimpleServer::TSimpleServer<apache::thrift::TProcessor>(boost::shared_ptr<apache::thrift::TProcessor> const&, boost::shared_ptr<apache::thrift::transport::TServerTransport> const&, boost::shared_ptr<apache::thrift::transport::TTransportFactory> const&, boost::shared_ptr<apache::thrift::protocol::TProtocolFactory> const&, boost::enable_if<boost::is_convertible<apache::thrift::TProcessor*, apache::thrift::TProcessor*>:: type , void*>:: type )':
CppServer.cpp:(.text._ZN6apache6thrift6server13TSimpleServerC2INS0_10TProcessorEEERKN5boost10shared_ptrIT_EERKNS6_INS0_9transport16TServerTransportEEERKNS6_INSB_17TTransportFactoryEEERKNS6_INS0_8protocol16TProtocolFactoryEEENS5_9enable_ifINS5_14is_convertibleIPS7_PS4_E4typeEPvE4typeE[_ZN6apache6thrift6server13TSimpleServerC5INS0_10TProcessorEEERKN5boost10shared_ptrIT_EERKNS6_INS0_9transport16TServerTransportEEERKNS6_INSB_17TTransportFactoryEEERKNS6_INS0_8protocol16TProtocolFactoryEEENS5_9enable_ifINS5_14is_convertibleIPS7_PS4_E4typeEPvE4typeE]+0x3a): undefined reference to `vtable  for  apache::thrift::server::TSimpleServer'
/tmp/ccBCSkj6 .o: In  function  `shared::SharedServiceClient::recv_getStruct(shared::SharedStruct&)':
SharedService.cpp:(.text+0x6f3): undefined reference to `apache::thrift::TApplicationException:: read (apache::thrift::protocol::TProtocol*)'
/tmp/ccBCSkj6 .o: In  function  `shared::SharedServiceProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)':
SharedService.cpp:(.text+0xb05): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccBCSkj6 .o: In  function  `shared::SharedServiceProcessor::process_fn(apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, int, void*)':
SharedService.cpp:(.text+0xd87): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccBCSkj6 .o: In  function  `shared::SharedServiceProcessor::process_getStruct(int, apache::thrift::protocol::TProtocol*, apache::thrift::protocol::TProtocol*, void*)':
SharedService.cpp:(.text+0x1409): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const'
/tmp/ccdeMoGV .o: In  function  `tutorial::CalculatorClient::recv_ping()':
Calculator.cpp:(.text+0x12af): undefined reference to `apache::thrift::TApplicationException:: read (apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV .o: In  function  `tutorial::CalculatorClient::recv_add()':
Calculator.cpp:(.text+0x173b): undefined reference to `apache::thrift::TApplicationException:: read (apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV .o: In  function  `tutorial::CalculatorClient::recv_calculate()':
Calculator.cpp:(.text+0x1c7b): undefined reference to `apache::thrift::TApplicationException:: read (apache::thrift::protocol::TProtocol*)'
/tmp/ccdeMoGV .o: In  function  `tutorial::CalculatorProcessor::process(boost::shared_ptr<apache::thrift::protocol::TProtocol>, boost::shared_ptr<apache::thrift::protocol::TProtocol>, void*)':
Calculator.cpp:(.text+0x227f): undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const

解决:      把-lthrift放到后面去

修改后的Makefile

1
2
3
4
5
6
7
8
9
10
11
BOOST_DIR =  /usr/local/boost/include/boost-1_33_1/
THRIFT_DIR =  /usr/local/include/thrift
LIB_DIR =  /usr/local/lib
GEN_SRC = .. /gen-cpp/SharedService .cpp .. /gen-cpp/shared_types .cpp .. /gen-cpp/tutorial_types .cpp .. /gen-cpp/Calculator .cpp
default: server client
server: CppServer.cpp
        g++ -DHAVE_NETINET_IN_H -o CppServer -I${THRIFT_DIR} -I${BOOST_DIR}  -I.. /gen-cpp  -L${LIB_DIR} CppServer.cpp ${GEN_SRC} -lthrift
client: CppClient.cpp
        g++ -DHAVE_NETINET_IN_H -o CppClient -I${THRIFT_DIR} -I${BOOST_DIR}  -I.. /gen-cpp  -L${LIB_DIR} CppClient.cpp ${GEN_SRC} -lthrift
clean:
        $(RM) -r CppClient CppServer


9、运行

再次执行make,运行成功,得到可执行程序CppServer和CppClient

在两个窗口依次运行./CppServer和./CppClient

输出如下:

1
2
3
4
5
6
$ . /CppClient  
ping ()
1+1=2
InvalidOperation: Cannot divide by 0
15-10=5
Check log: 5


1
2
3
4
5
6
7
$ . /CppServer  
Starting the server...
ping ()
add(1,1)
calculate(1,{4,1,0})
calculate(1,{2,15,10})
getStruct(1)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值