ubuntu下thrift的安装和运行

本文详细介绍了在Ubuntu系统中安装Thrift并使用C++语言进行网络编程的步骤。首先,从官网下载Thrift,然后通过特定命令安装依赖。在解压后的目录下执行编译命令。在运行教程示例时,可能遇到'uint32_t'不是类型名的错误,这通常是Makefile问题。解决方法是调整Makefile,确保正确的编译选项。参考Apache Thrift官方文档和Stack Overflow上的解决方案可以解决此类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

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时可能会遇到下面的错误:

  1. 错误: ‘uint32_t’不是一个类型名
  2. 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/

http://thrift.apache.org/docs/install/ubuntu/

http://stackoverflow.com/questions/9956861/thrift-cpp-sample-code-compile-error

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值