Thrift 跨語言 RPC 框架編譯依賴鏈長 (Boost、OpenSSL、libevent),版本不匹配、缺頭文件、malloc 重定義等問題頻發。本文提供經驗證的 全自動化安裝腳本,含關鍵 config.h 補丁、CPPFLAGS/LDFLAGS 繞過 OpenSSL 檢測缺陷、組件裁剪 (僅保留 C++/Python 等核心語言)。
.tar.gz 放同級目錄 $PROJECT_HOME/third-party/。if test $? -ne 0; then exit 1; fi 保證鏈路可靠。\033[0;32;34m 綠色標題,直觀追蹤進度。install_thrift.sh#!/bin/sh
# 一鍵安裝 thrift-0.9.0 及依賴
# 作者: 飛月 (mooon) https://code.google.com/p/mooon
PROJECT_HOME=$HOME/iflow
THIRD_PARTY_HOME=$PROJECT_HOME/third-party
boost=boost_1_52_0
openssl=openssl-1.0.1c
libevent=libevent-2.0.19-stable
thrift=thrift-0.9.0
cd $THIRD_PARTY_HOME
# ---------- 1. Boost ----------
printf "\n\033[0;32;34minstalling boost\033[m\n"
tar xzf $boost.tar.gz
cd $boost
./bootstrap.sh
if test $? -ne 0; then exit 1; fi
./b2 install --prefix=$THIRD_PARTY_HOME/boost
printf "\n\033[0;32;34m./b2 install return $?\033[m\n"
cd -
# ---------- 2. OpenSSL ----------
printf "\n\033[0;32;34minstalling openssl\033[m\n"
tar xzf $openssl.tar.gz
cd $openssl
./config --prefix=$THIRD_PARTY_HOME/openssl shared threads
if test $? -ne 0; then exit 1; fi
make
if test $? -ne 0; then exit 1; fi
make install
cd -
# ---------- 3. libevent ----------
printf "\n\033[0;32;34minstalling libevent\033[m\n"
tar xzf $libevent.tar.gz
cd $libevent
./configure --prefix=$THIRD_PARTY_HOME/libevent
if test $? -ne 0; then exit 1; fi
make
if test $? -ne 0; then exit 1; fi
make install
cd -
# ---------- 4. Thrift (核心補丁區) ----------
printf "\n\033[0;32;34minstalling thrift\033[m\n"
tar xzf $thrift.tar.gz
cd $thrift
# 關鍵:用 CPPFLAGS/LDFLAGS 直接指定 OpenSSL 路徑,繞過 --with-openssl 的 libcrypto 檢測 bug
./configure --prefix=$THIRD_PARTY_HOME/thrift \
--with-boost=$THIRD_PARTY_HOME/boost \
--with-libevent=$THIRD_PARTY_HOME/libevent \
CPPFLAGS="-I$THIRD_PARTY_HOME/openssl/include" \
LDFLAGS="-ldl -L$THIRD_PARTY_HOME/openssl/lib" \
--with-qt4=no --with-c_glib=no --with-csharp=no \
--with-java=no --with-erlang=no --with-python=no \
--with-perl=no --with-ruby=no --with-haskell=no \
--with-go=no --with-d=no
if test $? -ne 0; then exit 1; fi
# 補丁 config.h:啟用 malloc/realloc,禁用 rpl_ 前綴 (防止編譯錯誤)
sed -i -e 's!#define HAVE_MALLOC 0!#define HAVE_MALLOC 1!' config.h
sed -i -e 's!#define HAVE_REALLOC 0!#define HAVE_REALLOC 1!' config.h
sed -i -e 's!#define malloc rpl_malloc!/*#define malloc rpl_malloc*/!' config.h
sed -i -e 's!#define realloc rpl_realloc!/*#define realloc rpl_realloc*/!' config.h
make
if test $? -ne 0; then exit 1; fi
make install
cd -
printf "\n\033[0;32;34minstall SUCCESS\033[m\n"
| 問題 | 原因 | 腳本對策 |
|---|---|---|
Error: libcrypto required | configure --with-openssl 邏輯缺陷,未正確傳遞庫路徑 | 改用 CPPFLAGS/LDFLAGS 硬指定 |
malloc/realloc not declared (make 階段) | Thrift 0.9.0 自帶 malloc 替換邏輯與系統頭文件衝突 | sed 修改 config.h 強制啟用系統 malloc |
| 編譯出大量語言綁定 (Java/Python/Go…) | 默認全部啟用,耗時且多依賴 | --with-xxx=no 裁剪至極簡 |
mkdir -p ~/iflow/third-party && cd $_wget https://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.tar.gzwget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1c.tar.gzwget https://github.com/libevent/libevent/releases/download/release-2.0.19-stable/libevent-2.0.19-stable.tar.gzwget https://archive.apache.org/dist/thrift/0.9.0/thrift-0.9.0.tar.gzinstall_thrift.sh 放入同目錄。chmod +x install_thrift.sh && ./install_thrift.sh~/iflow/third-party/{boost,openssl,libevent,thrift},編譯項目時:CXXFLAGS="-I.../thrift/include -I.../boost/include"LDFLAGS="-L.../thrift/lib -L.../boost/lib -lthrift -lboost_system -lssl -lcrypto -levent"Thrift 0.9.0 已停止維護 (2013 年)。新項目建議直接 yum/apt install thrift-devel (通常 0.13+) 或從 GitHub 主分支 構建,享受 C++11/14/17 支援、改進的編譯系統 (CMake) 與安全修補。
來源:飛月博客