编译 C++客户端之前首先需要本地编译 Thrift 库,compile-tools 模块负责编译 Thrift,之后再编译 client-cpp。
Mac 环境下预安装了 Bison 2.3 版本,但该版本过低不能够用来编译 Thrift。使用 Bison 2.3 版本是会报以下错误:
invalid directive: '%code'
使用下面 brew 命令更新 bison 版本
brew install bison brew link bison --force
添加环境变量:
echo 'export PATH="/usr/local/opt/bison/bin:$PATH"' >> ~/.bash_profile
确保安装较新的 Boost 版本:
brew install boost
brew link boost
确保 openssl 库已安装,默认的 openssl 头文件路径为"/usr/local/opt/openssl/include" 如果在编译 Thrift 过程中出现找不到 openssl 的错误,尝试添加
-Dopenssl.include.dir=""
Linux 下需要确保 g++已被安装。
Ubuntu 20:
一条命令安装所有依赖库:
sudo apt-get install gcc-9 g++-9 libstdc++-9-dev bison flex libboost-all-dev libssl-dev zlib1g-dev
CentOS 7.x:
在centos 7.x里,可用yum命令安装部分依赖。
sudo yum install bison flex openssl-devel
使用yum安装的GCC、boost版本过低,在编译时会报错,需自行安装或升级。
保证你的 Windows 系统已经搭建好了完整的 C/C++的编译构建环境。可以是 MSVC,MinGW 等。
如使用 MS Visual Studio,在安装时需要勾选 Visual Studio C/C++ IDE and compiler(supporting CMake, Clang, MinGW)。
CMake 官网下载地址 https://cmake.org/download/
CMake 需要根据不同编译平台使用不同的生成器。CMake 支持的生成器列表如下 (cmake --help的结果):
Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10 2010 [arch] = Generates Visual Studio 2010 project files.
Optional [arch] can be "Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
Borland Makefiles = Generates Borland makefiles.
* NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Unix Makefiles = Generates standard UNIX makefiles.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
Ninja = Generates build.ninja files.
Ninja Multi-Config = Generates build-<Config>.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
编译 client-cpp 时的 mvn 命令中添加 -Dcmake.generator="" 选项来指定使用的生成器名称。
mvn package -Dcmake.generator="Visual Studio 15 2017 [arch]"
Windows 版的 Flex 和 Bison 可以从 SourceForge 下载:https://sourceforge.net/projects/winflexbison/
下载后需要将可执行文件重命名为 flex.exe 和 bison.exe 以保证编译时能够被找到,添加可执行文件的目录到 PATH 环境变量中。
Boost 官网下载地址 https://www.boost.org/users/download/
依次执行 bootstrap.bat 和 b2.exe,本地编译 boost
bootstrap.bat b2.exe
为了帮助 CMake 本地安装好的 Boost,在编译 client-cpp 的 mvn 命令中需添加:
-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}
openssl 官网源码下载地址 https://www.openssl.org/source/
二进制文件下载地址 http://slproweb.com/products/Win32OpenSSL.html
在编译前,需要确定cmake,flex,bison,openssl都加入了PATH。
Maven 命令中添加"-P client-cpp" 选项编译 client-cpp 模块。client-cpp 需要依赖编译好的 thrift,即 compile-tools 模块。
mvn package -P compile-cpp -pl example/client-cpp-example -am -DskipTest
编译前需额外添加Boost相关的参数:
-Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder}
需指定cmake的generator,例如:
-Dcmake.generator="Visual Studio 15 2017 [arch]"
完整编译C++客户端命令如下:
mvn package -P compile-cpp -pl client-cpp,server,example/client-cpp-example -am -Dcmake.generator="your cmake generator" -Dboost.include.dir=${your boost header folder} -Dboost.library.dir=${your boost lib (stage) folder} -DskipTests
编译成功后,打包好的 zip 文件将位于:“client-cpp/target/client-cpp-${project.version}-cpp-${os}.zip”
解压后的目录结构如下图所示 (Mac):
. +-- client | +-- include | +-- Session.h | +-- TSIService.h | +-- rpc_types.h | +-- rpc_constants.h | +-- thrift | +-- thrift_headers... | +-- lib | +-- libiotdb_session.dylib
本地 Maven 编译 Thrift 时如出现以下链接的问题,可以尝试将 xcode-commandline 版本从 12 降低到 11.5
Maven 编译 Thrift 时需要使用 wget 下载远端文件,可能出现以下报错:
Failed to delete cached file C:\Users\Administrator\.m2\repository\.cache\download-maven-plugin\index.ser
解决方法: