安裝
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