安装
1. 去官网下载
2. 根据系统以及所需语言安装所需软件
ubuntu c++语言参考以下命令安装:
sudo apt-get install libboost-dev libboost-test-dev libboost-program-options-dev libevent-dev automake libtool flex bison pkg-config g++ libssl-dev
3. 在thrift解压的目录下,执行以下:
$./configure $make $sudo make install
使用
可以运行tutorial的例子:
1. 生成相应语言的代码
thrift --gen <language> <Thrift filename>
2. 编译
在运行生成c++的代码后,运行cpp目录下面的make时可能会遇到下面的错误:
- 错误: ‘uint32_t’不是一个类型名
- undefined reference to `apache::thrift::TApplicationException::write(apache::thrift::protocol::TProtocol*) const
主要是Makefile有些问题,可参照如下的Makefile(拷贝时注意Makefile的tab):
# # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # THRIFT_VER =thrift-0.9.0 USR_DIR =${HOME}/usr THRIFT_DIR =${USR_DIR}/${THRIFT_VER} INCS_DIRS =-I${USR_DIR}/include -I${THRIFT_DIR}/include/thrift LIBS_DIRS =-L${USR_DIR}/lib -L${USR_DIR}/${THRIFT_VER}/lib CPP_DEFS =-D=HAVE_NETINET_IN_H CPP_OPTS =-Wall -O2 LIBS =-lthrift GEN_SRC = ../gen-cpp/SharedService.cpp \ ../gen-cpp/shared_types.cpp \ ../gen-cpp/tutorial_types.cpp \ ../gen-cpp/Calculator.cpp GEN_INC = -I../gen-cpp default: server client server: CppServer.cpp g++ ${CPP_OPTS} ${CPP_DEFS} -o CppServer ${GEN_INC} ${INCS_DIRS} CppServer.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS} client: CppClient.cpp g++ ${CPP_OPTS} ${CPP_DEFS} -o CppClient ${GEN_INC} ${INCS_DIRS} CppClient.cpp ${GEN_SRC} ${LIBS_DIRS} ${LIBS} clean: $(RM) -r CppClient CppServer
参考:
http://thrift.apache.org/docs/install/ubuntu/
http://stackoverflow.com/questions/9956861/thrift-cpp-sample-code-compile-error