以下是在Linux系统上编译和安装Thrift 0.16的详细步骤:


1. 安装依赖项

根据系统类型选择对应的命令安装依赖:

Ubuntu/Debian
sudo apt update
sudo apt install -y \
    automake bison build-essential curl flex g++ \
    libboost-dev libboost-filesystem-dev libboost-system-dev \
    libboost-test-dev libevent-dev libssl-dev libtool make pkg-config \
    python3-dev
CentOS/RHEL
sudo yum groupinstall -y "Development Tools"
sudo yum install -y \
    automake bison flex boost-devel libevent-devel openssl-devel \
    python3-devel
    # 缺包
    yum install protobuf-compiler -y
    # doris 教程
    yum install -y autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++

2. 下载Thrift 0.16源码

方式1:直接下载压缩包
wget https://archive.apache.org/dist/thrift/0.16.0/thrift-0.16.0.tar.gz
tar -xzvf thrift-0.16.0.tar.gz
cd thrift-0.16.0
方式2:从GitHub克隆(需Git)
git clone https://github.com/apache/thrift.git
cd thrift
git checkout v0.16.0  # 切换到指定版本

3. 配置编译选项

./bootstrap.sh  # 若从Git克隆,需生成configure脚本
./configure \
    --prefix=/usr/local \  # 指定安装路径(默认/usr/local)
    --without-java \       # 可选:禁用不需要的语言支持
    --without-python       # 若无需Python可禁用
# 若需其他配置,请查看./configure --help
# doris官网的教程
# Linux:下载源码包:
wget https://archive.apache.org/dist/thrift/0.16.0/thrift-0.16.0.tar.gz
# 安装依赖:
yum install -y autoconf automake libtool cmake ncurses-devel openssl-devel lzo-devel zlib-devel gcc gcc-c++
tar zxvf thrift-0.16.0.tar.gz
cd thrift-0.16.0
./configure --without-tests
make
make install

4. 编译并安装

make -j$(nproc)           # 使用多核加速编译
sudo make install         # 安装到系统目录(需要权限)

5. 验证安装

# 手动软连接到环境变量
ls /usr/local/bin|grep thrift
chmod +x /usr/local/bin/thrift
ln -s /usr/local/bin/thrift /usr/bin/thrift
ls /usr/bin/thrift
# proto也是手动环境变量
ln -s /usr/local/bin/thrift ${doris-home}/thirdparty/installed/bin/thrift
ln -s /usr/bin/protoc ${doris-home}/thirdparty/installed/bin/protoc
# 查看版本
thrift --version
# 输出应为:Thrift version 0.16.0

6. 环境配置(可选)

如果安装到非系统路径(如/opt/thrift),需手动添加环境变量:

echo 'export PATH=/opt/thrift/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/opt/thrift/lib:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc

常见问题解决

  1. 依赖缺失错误
    根据configure或make阶段的报错信息,安装对应的开发包。

    • 例如报错libboost not found:Ubuntu下安装libboost-all-dev,CentOS下安装boost-devel。
  2. 权限问题
    安装到系统目录时需使用sudo make install。

  3. 版本冲突
    若系统已存在旧版Thrift,建议卸载或通过--prefix指定新路径安装。


测试Thrift

编写测试文件example.thrift:

service ExampleService {
  string hello(1:string name)
}

生成代码验证编译器是否正常:

thrift -r --gen cpp example.thrift  # 生成C++代码

完成以上步骤后,Thrift 0.16应成功安装并可用。

更多推荐