首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

一鍵編譯安裝 Thrift 0.9.0 及依賴 (Boost/OpenSSL/libevent) Bash 腳本

     編輯:關於C語言

Thrift 跨語言 RPC 框架編譯依賴鏈長 (Boost、OpenSSL、libevent),版本不匹配、缺頭文件、malloc 重定義等問題頻發。本文提供經驗證的 全自動化安裝腳本,含關鍵 config.h 補丁、CPPFLAGS/LDFLAGS 繞過 OpenSSL 檢測缺陷、組件裁剪 (僅保留 C++/Python 等核心語言)。

腳本特性

  • 目錄約定:腳本與四個 .tar.gz 放同級目錄 $PROJECT_HOME/third-party/。
  • 版本鎖定:Boost 1.52.0 / OpenSSL 1.0.1c / libevent 2.0.19 / Thrift 0.9.0 — 經驗證相容組合。
  • 失敗即停**:每步 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 requiredconfigure --with-openssl 邏輯缺陷,未正確傳遞庫路徑改用 CPPFLAGS/LDFLAGS 硬指定
malloc/realloc not declared (make 階段)Thrift 0.9.0 自帶 malloc 替換邏輯與系統頭文件衝突sed 修改 config.h 強制啟用系統 malloc
編譯出大量語言綁定 (Java/Python/Go…)默認全部啟用,耗時且多依賴--with-xxx=no 裁剪至極簡

使用步驟

  1. 建立目錄並下載源碼包:
    mkdir -p ~/iflow/third-party && cd $_
    wget https://sourceforge.net/projects/boost/files/boost/1.52.0/boost_1_52_0.tar.gz
    wget https://www.openssl.org/source/old/1.0.1/openssl-1.0.1c.tar.gz
    wget https://github.com/libevent/libevent/releases/download/release-2.0.19-stable/libevent-2.0.19-stable.tar.gz
    wget https://archive.apache.org/dist/thrift/0.9.0/thrift-0.9.0.tar.gz
  2. 將腳本保存為 install_thrift.sh 放入同目錄。
  3. chmod +x install_thrift.sh && ./install_thrift.sh
  4. 安裝完成後,所有庫位於 ~/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) 與安全修補。

來源:飛月博客

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved