目录
thrift在windows上编译(保姆级教程)
Thrift是一个轻量级的,与语言无关的软件堆栈,具有用于RPC的相关代码生成机制。Thrift为数据传输,数据序列化和应用程序级处理提供了干净的抽象。Thrift最初由Facebook开发,现在作为Apache项目开源。Apache Thrift是一组代码生成工具,允许开发人员通过在简单的定义文件中定义数据类型和服务接口来构建RPC客户端和服务器。给定该文件作为输入,将生成代码以构建可跨编程语言无缝通信的RPC客户端和服务器。
最近在使用Thrift, 因为一直都在使用vs开发,所以想使用vs编译Thrift。在编译过程中遇到很多坑,在网上查了一圈每个回答试过都不能正常编译。故此在这里记录下,也给初次使用Thrift的同学一个参考。
遇坑记录(共勉(ง •_•)ง)
环境:boost_1_55_0, thrift-0.14.1, libevent-2.0.21-stable,OpenSSL-Win32,win_flex_bison-latest,vs2013
在这个环境下我编译boost_1_55_0(编译了很长时间,后面thrift编译成功中发现并不需要编译boost库),libevent-2.0.21-stable 库不需要编译网上很多教程都说需要编译,OpenSSL-Win32可直接下在安装包这可以省去编译openssl, win_flex_bison-latest在thrift-0.14.1\compiler\cpp\compiler.sln 解决方案里使用这个只要修改“生成事件”里的 flex -o "src\thrift\thriftl.cc" src/thrift/thriftl.ll && bison -y -o "src\thrift\thrifty.cc" --defines="src\thrift\thrifty.hh" src/thrift/thrifty.yy 将flex和bison路径换成win_flex_bison-latest目录下的文件即可
在这个环境下出现大量的 “BOOST_NOEXCEPT”: 未知重写说明符错误,查大量资料未解决。最后决定修改代码将NOEXCEPT删除libthriftnb编过,libthrift未编过出现
“ 严重性 代码 说明 项目 文件 行 禁止显示状态 警告 C4273 “manualOpenSSLInitialization_”: dll 链接不一致 libthrift e:\work\virtualapp\thrift-0.14.11\thrift-0.14.1\lib\cpp\src\thrift\transport\tsslsocket.cpp 856 ” 错误(其实是导入导出宏错误之后添加thrift_EXPORTS解决) 并且出现“BOOST_NOEXCEPT”未知重写说明符错误 -》奔溃::>_<::,还出现了大量模板的错误, 。。。。。猜测是vs版本不对。安装vs2015后解决。后面文章是我在vs2015编译成功的完整过程。
(不想看那么多的同学直接看这)
-
环境准备
-
boost_1_55_0
https://www.boost.org/users/history/version_1_55_0.html -
thrift-0.14.1
http://www.apache.org/dyn/closer.cgi?path=/thrift/0.14.1/thrift-0.14.1.tar.gz -
libevent-2.0.21-stable
https://libevent.org/ -
OpenSSL-Win32
https://sourceforge.net/projects/openssl-for-windows/ -
win_flex_bison-latest
https://sourceforge.net/projects/winflexbison/ -
vs2015
ed2k://|file|cn_visual_studio_enterprise_2015_x86_x64_dvd_6846222.iso|4172560384|E558149A422E9FBF7D1D37FB0A2F1F53|/
下载好以上的资源,将他们解压放到同一个目录下。
-
编译thrift-0.14.11\thrift-0.14.1\lib\cpp\thrift.sln
将libthrift和libthriftnb两个项目的包含目录都改成下面的路径,注意将他们换成你们自己的路径
$(ProjectDir)src\;$(ProjectDir)src\thrift\windows\;E:\virtualapp\boost_1_55_0\boost_1_55_0;E:\virtualapp\boost_1_55_0\boost_1_55_0\boost;C:\OpenSSL-Win32\include;E:\virtualapp\libevent-2.0.21-stable\libevent-2.0.21-stable\include\;E:\virtualapp\libevent-2.0.21-stable\libevent-2.0.21-stable\;E:\virtualapp\libevent-2.0.21-stable\libevent-2.0.21-stable\WIN32-Code\;$(IncludePath)
libthrift项目的预处理器需要加上 “thrift_EXPORTS”和将“E:\virtualapp\thrift-0.14.11\thrift-0.14.1\lib\cpp\src\thrift\transport\THttpClient.cpp”下的flush()函数改成下面那样
void THttpClient::flush() {
resetConsumedMessageSize();
// Fetch the contents of the write buffer
uint8_t* buf;
uint32_t len;
writeBuffer_.getBuffer(&buf, &len);
// Construct the HTTP header
std::ostringstream h;
h << "POST " << path_ << " HTTP/1.1" << CRLF << "Host: " << host_ << CRLF
<< "Content-Type: application/x-thrift" << CRLF << "Content-Length: " << len << CRLF
<< "Accept: application/x-thrift" << CRLF << "User-Agent: Thrift/" << 14.11
<< " (C++/THttpClient)" << CRLF << CRLF;
string header = h.str();
if (header.size() > (std::numeric_limits<uint32_t>::max)())
throw TTransportException("Header too big");
// Write the header, then the data, then flush
transport_->write((const uint8_t*)header.c_str(), static_cast<uint32_t>(header.size()));
transport_->write(buf, len);
transport_->flush();
// Reset the buffer and header variables
writeBuffer_.resetBuffer();
readHeaders_ = true;
}
其实就改了thrift版本号的宏,如果其他地方也有同样的改。
-
修改boost库boost/config/compiler/visualc.hpp文件
修改最后的版本判断条件 我是直接 #if 0 不判断vs版本。
读者可参考如下链接修改当然也可以和我一样了 https://stackoverflow.com/questions/30760889/unknown-compiler-version-while-compiling-boost-with-msvc-14-0-vs-2015
至此,我们可以正常编译了。
为了方便读者我将可编译过的文件上传到github上,欢迎读者关注微信公众号“智子开天科技”获取资料。
- END -