免费获取学习方案
ARTICLE DETAIL

资讯详情

深耕编程基础知识与建站技术分享的一线实战洞察。

Apache Thrift 在 CentOS 上的源码编译安装完整指南

Apache Thrift 在 CentOS 上的源码编译安装完整指南 Apache Thrift 在 CentOS 上的源码编译安装完整指南【免费下载链接】thriftApache Thrift项目地址: https://gitcode.com/gh_mirrors/thrift2/thrift导读本文基于仓库中的官方安装文档 doc/install/centos.md系统梳理在 CentOS 6.5 最小化安装环境下从源码构建 Apache Thrift 的完整流程从系统更新、平台工具链准备、autoconf/automake/bison 升级到 C 库依赖Boost/libevent/OpenSSL/zlib安装再到 IDL 编译器与各语言库的编译、安装与验证。读完本文你将掌握在 RHEL/CentOS 系发行版上独立完成 Thrift 全量构建、仅构建编译器--enable-libsno以及运行make check测试的能力并能结合仓库源码理解每一步背后的构建系统原理。一、为什么在 CentOS 上从源码构建 Apache ThriftApache Thrift 的构建系统以 GNU autotoolsautoconf automake libtool为基础对工具链版本有明确要求。以 CentOS 6.5 为代表的旧发行版自带的 autoconf/automake/bison 版本往往偏低无法满足仓库构建脚本的最低版本检查因此官方安装指南建议先升级这些工具再进入 Thrift 本身的构建流程。从仓库源码可以印证这些版本约束configure.ac 声明AC_PREREQ(2.65)要求 autoconf ≥ 2.65并使用AM_INIT_AUTOMAKE([1.13 ...])要求 automake ≥ 1.13顶层 bootstrap.sh 在执行aclocal/autoconf/automake之前会先检查 automake 版本automake version ... is too old (need 1.13 or later)configure.ac 调用AC_PROG_BISON(2.5)要求 bison ≥ 2.5 来生成语法解析器编译器的词法/语法分析器由 flex 与 bison 从 compiler/cpp/src/thrift/thriftl.ll 和 compiler/cpp/src/thrift/thrifty.yy 生成见 compiler/cpp/CMakeLists.txt因此 lex/yaccflex/bison是构建编译器的硬性前置条件。这也解释了为什么本文档以及 doc/install/README.md 中的 Requirements for building from source 一节会把 autoconf 2.65、automake 1.13、libtool、flex/bison 列为构建前提。适用范围说明文档针对 CentOS 6.5 最小化安装编写同时说明这些步骤同样适用于 Apache Thrift 0.9.2 及之后的发布版本仓库当前版本为 0.21.0见 configure.ac 的AC_INIT([thrift], [0.21.0])。CentOS 6 系列已于 2020 年停止维护如需在更新的 CentOS/RHEL 版本上安装可使用yum install直接安装新版工具链其余流程相同。二、更新系统与安装平台开发工具2.1 更新系统以 root 或具有 sudo 权限的用户执行先同步系统软件包索引并升级sudo yum -y update2.2 安装开发工具组CentOS 通过groupinstall安装整套编译工具链gcc、g、make、libtool 等sudo yum -y groupinstall Development Tools该软件组提供 GNU 编译器套件与 make是后续源码编译的基础。若你的发行版使用 dnfCentOS 8对应命令为sudo dnf groupinstall Development Tools。三、升级 autoconf / automake / bisonCentOS 6.5 自带的 autoconf2.63、automake1.11与 bison 版本均低于 Thrift 构建要求需要从源码安装新版。仓库构建脚本的最低版本要求可参考前文工具Thrift 要求本文档安装版本检查位置autoconf≥ 2.652.69configure.acautomake≥ 1.131.14bootstrap.shbison≥ 2.52.5.1configure.ac3.1 安装 wget后续下载源码包需要 wgetsudo yum install -y wget3.2 升级 autoconfwget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz tar xvf autoconf-2.69.tar.gz cd autoconf-2.69 ./configure --prefix/usr make sudo make install cd ..--prefix/usr将新版 autoconf 安装到系统路径覆盖旧版本避免 PATH 冲突。3.3 升级 automakewget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz tar xvf automake-1.14.tar.gz cd automake-1.14 ./configure --prefix/usr make sudo make install cd ..automake 1.14 满足 bootstrap.sh 中 we require automake 1.13 or later 的硬性检查。若安装后automake --version仍显示旧版本请确认PATH中/usr/bin优先于旧安装目录。3.4 升级 bisonwget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz tar xvf bison-2.5.1.tar.gz cd bison-2.5.1 ./configure --prefix/usr make sudo make install cd ..bison 负责从 compiler/cpp/src/thrift/thrifty.yy 生成 Thrift IDL 语法解析器版本不足将直接导致configure失败AC_PROG_BISON(2.5)会拒绝旧版本。3.5 验证工具链autoconf --version | head -1 automake --version | head -1 bison --version | head -1确认三个版本号分别不低于 2.65、1.13、2.5 后再继续后续步骤。四、安装 C 语言库依赖可选所有语言代码生成都依赖 Apache Thrift IDL 编译器而前三节已把编译编译器所需的全部工具装齐。如果你只需要编译器可以跳过本节直接进入第五章的构建步骤。但若要在 C 中开发 Thrift 客户端/服务器构建libthrift共享库还需要以下依赖4.1 安装 libevent / zlib / openssl 开发包sudo yum -y install libevent-devel zlib-devel openssl-devellibevent-devel构建非阻塞服务器TNonblockingServer所需见 doc/install/README.md 中 C 一节 libevent (optional, to build the nonblocking server)zlib-devel启用压缩传输/协议支持openssl-devel启用 TLS/SSL 安全传输支持。这些库在 configure.ac 对应的 CMake 构建中同样被检测WITH_LIBEVENT AND WITH_ZLIB AND WITH_OPENSSL缺失时对应特性会被禁用。4.2 升级 Boost ≥ 1.56CentOS 6.5 自带 Boost 版本过旧无法满足 C 库与测试的编译要求doc/install/README.md 明确要求 Boost 1.56.0需从源码安装wget http://sourceforge.net/projects/boost/files/boost/1.56.0/boost_1_56_0.tar.gz tar xvf boost_1_56_0.tar.gz cd boost_1_56_0 ./bootstrap.sh sudo ./b2 install./b2 install默认将 Boost 头文件安装到/usr/local/include、库文件安装到/usr/local/lib。若后续./configure无法找到 Boost可通过CPPFLAGS/LDFLAGS或--with-boost指定路径仓库 aclocal/ax_boost_base.m4 会依次在/usr、/usr/local、/opt、/opt/local下探测 Boost 安装位置。五、构建并安装 Apache Thrift IDL 编译器5.1 获取源码并引导构建系统git clone https://github.com/apache/thrift.git cd thrift ./bootstrap.sh也可以直接使用本仓库当前版本 0.21.0./bootstrap.sh是 Thrift 特有的引导脚本它依次执行autoscan、libtoolize、aclocal -I ./aclocal、autoheader、autoconf、automake --copy --add-missing等步骤见 bootstrap.sh把仓库中的configure.ac、Makefile.am等元数据展开成可执行的configure脚本与 Makefile运行前请确认libtoolize/glibtoolize可用脚本在找不到时会直接报错退出bootstrap.sh。5.2 配置构建./configure --with-luano--with-luano用于显式禁用 Lua 语言库的构建。仓库 configure.ac 显示 Lua 库默认启用AX_THRIFT_LIB(lua, [Lua], yes)但要求 Lua ≥ 5.2AX_PROG_LUA(5.2,...)并需要头文件与链接库在 CentOS 6.5 上 Lua 版本通常不满足要求因此文档统一使用--with-luano关闭避免configure探测失败或构建中断。这一步还会完成所有语言库的可用性探测C、Java、Python、Ruby、Go、Node.js、Rust、Erlang、PHP、Perl、D、Haxe、Delphi、NetStd、Swift 等configure结束时输出的摘要如 Building Lua Library ......... : no会逐项列出各语言库是否将被构建。5.3 编译与安装make sudo make install顶层 Makefile.am 定义SUBDIRS compiler/cpp lib即一次构建同时产出编译器与各语言库编译完成后编译器可执行文件为compiler/cpp/thrift位于compiler/cpp/目录下即 compiler/cpp/CMakeLists.txt 中add_executable生成的thriftsudo make install会将编译器安装到系统路径/usr/local/bin/thrift安装后可用如下命令验证版本thrift --version六、常用构建变体与测试6.1 仅构建 IDL 编译器如果只需要thrift编译器用于生成各语言代码不需要任何语言运行时库可跳过第四节依赖并改用./configure --enable-libsno make sudo make install--enable-libsno关闭所有语言库的构建。仓库 configure.ac 显示该开关默认值为yes[defaultyes]一旦置为noC、C_glib、Java、Python、Ruby、PHP、Go、Node.js、Dart、Erlang、Lua、Rust、NetStd、Kotlin、Swift 等库的构建条件会被统一置为nomake只产出编译器本身显著缩短构建时间。6.2 运行测试make checkmake check会执行仓库内置的测试套件顶层 Makefile.am 在启用测试WITH_TESTS时会把test目录纳入构建测试资源位于 test 目录含ThriftTest.thrift、DebugProtoTest.thrift等 IDL 定义及各语言客户端/服务器测试。注意make check需要各语言运行环境与库已正确构建若使用--enable-libsno或缺少某语言运行时对应语言的测试会被跳过或失败。七、安装后的验证与常见问题7.1 验证安装which thrift # 应输出 /usr/local/bin/thrift thrift --version # 应输出 Thrift version x.y.z7.2 常见问题排查现象原因与对策bootstrap.sh报 automake ... is too oldautomake 1.13按第三节重新安装 automake 1.14configure报 bison 版本错误bison 2.5按 3.4 升级configure找不到 BoostBoost 未安装或不在默认路径安装 1.56 或用CPPFLAGS/LDFLAGS指定路径Lua 相关探测失败导致 configure 报错使用--with-luano或安装 Lua 5.2 开发包make报缺少 libevent/zlib/openssl 头文件执行sudo yum -y install libevent-devel zlib-devel openssl-devel7.3 下一步安装完成后可以用thrift --gen 语言 xxx.thrift为任意支持的语言生成代码各语言生成器源码位于 compiler/cpp/src/thrift/generate参考 tutorial 目录下的各语言客户端/服务器示例如 tutorial/cpp/CppClient.cpp、tutorial/py/PythonServer.py编写第一个 Thrift 服务阅读 doc/install/debian.md 了解 Debian/Ubuntu 系的对应安装方式以及各语言库的可选依赖包清单Java 需 Gradle 8.4、Rust 需 rustc/cargo、Lua 需 lua5.3 等。【免费下载链接】thriftApache Thrift项目地址: https://gitcode.com/gh_mirrors/thrift2/thrift创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表