ubuntu16.04下thrift安装及遇到的问题

ubuntu下安装thrift

ubuntu 16.04
thrift 0.16.0

boost安装

方法一

#安装mpi库
sudo apt-get install mpi-default-dev
#支持正则表达式的UNICODE字符集
sudo apt-get install libicu-dev
#需要python的话
sudo apt-get install python-dev
#如果编译出现错误:bzlib.h: No such file or directory
sudo apt-get install libbz2-dev

解压之后

./bootstrap.sh
sudo ./b2
sudo ./b2 install

下载boost
https://sourceforge.net/projects/boost/files/latest/download

方法二

sudo apt-get update
sudo apt install libboost-all-dev
sudo apt install aptitude
aptitude search boost

安转好之后,默认安装目录在 /usr/include/boost

测试

创建一个main.cpp

#include <iostream>
#include <boost/array.hpp>

using namespace std;
int main(){
    
    
  boost::array<int, 4> arr = {
    
    {
    
    1,2,3,4}};
  cout << "hi" << arr[0];
  return 0;
}

编译

g++ -o stest main.cpp

执行

./stest
hi1

成功

安装thrift

thrfit官网,点这里

下载thrfit源码包 链接

下载后解压

wget http://www.apache.org/dyn/closer.cgi?path=/thrift/0.16.0/thrift-0.16.0.tar.gz
sudo tar -xvf thrift-0.16.0.tar.gz

安装

cd thrift-0.16.0.tar.gz
sudo ./configure
sudo make

编译过程中,报错

src/thrift/transport/TSSLSocket.cpp:44:30: fatal error: openssl/opensslv.h: No such file or directory
compilation terminated.
Makefile:1246: recipe for target 'src/thrift/transport/TSSLSocket.lo' failed
make[4]: *** [src/thrift/transport/TSSLSocket.lo] Error 1
make[4]: Leaving directory '/home/bing/bingCpp/thriftDemo/thrift-0.16.0/lib/cpp'
Makefile:1526: recipe for target 'all-recursive' failed
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory '/home/bing/bingCpp/thriftDemo/thrift-0.16.0/lib/cpp'
Makefile:569: recipe for target 'all-recursive' failed
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory '/home/bing/bingCpp/thriftDemo/thrift-0.16.0/lib'
Makefile:682: recipe for target 'all-recursive' failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory '/home/bing/bingCpp/thriftDemo/thrift-0.16.0'
Makefile:598: recipe for target 'all' failed
make: *** [all] Error 2

根据debian系统下的安装方法,需要安装一些依赖

# 依赖安装
sudo apt-get install automake bison flex g++ git libboost-all-dev libevent-dev libssl-dev libtool make pkg-config
# 再次make
sudo ./configure
sudo make

安装thrift

# 安装
$ sudo make install
# 查看版本
$ thrift -version
Thrift version 0.16.0

测试demo

1、 定义接口文件 hello.thrift

service HelloService
{
    
    
    void hello(1: string name);
}

2、编译生成cpp接口

thrift --gen cpp hello.thrift
# 自动创建了一个gen-cpp文件夹
cd gen-cpp
mkdir build
# 复制一份server框架
copy HelloService_server.skeleton.cpp server.cpp

在这里插入图片描述
打开server.cpp

// This autogenerated skeleton file illustrates how to build a server.
// You should copy it to another filename to avoid overwriting it.

#include "HelloService.h"
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/server/TSimpleServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TBufferTransports.h>

using namespace ::apache::thrift;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift::server;

class HelloServiceHandler : virtual public HelloServiceIf {
    
    
 public:
  HelloServiceHandler() {
    
    
    // Your initialization goes here
  }

  void hello(const std::string& name) {
    
    
    // Your implementation goes here
    printf("hello\n");
  }

};

int main(int argc, char **argv) {
    
    
  int port = 9090;
  ::std::shared_ptr<HelloServiceHandler> handler(new HelloServiceHandler());
  ::std::shared_ptr<TProcessor> processor(new HelloServiceProcessor(handler));
  ::std::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  ::std::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  ::std::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

  TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
  server.serve();
  return 0;
}

编译server.cpp

$ g++ -o ./build/server HelloService.cpp server.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift

出现报错This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options

In file included from /usr/include/c++/5/chrono:35:0,
                 from /usr/local/include/thrift/concurrency/Monitor.h:23,
                 from /usr/local/include/thrift/async/TConcurrentClientSyncInfo.h:24,
                 from HelloService.h:11,
                 from HelloService.cpp:7:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
 #error This file requires compiler and library support \
  ^
In file included from /usr/local/include/thrift/transport/TTransport.h:23:0,
                 from /usr/local/include/thrift/protocol/TProtocol.h:28,
                 from /usr/local/include/thrift/TProcessor.h:24,
                 from /usr/local/include/thrift/TDispatchProcessor.h:22,
                 from HelloService.h:10,
                 from HelloService.cpp:7:
/usr/local/include/thrift/Thrift.h:82:23: error: expected ‘;’ at end of member declaration
   virtual ~TException() noexcept override = default;

按提示加上选项-std=c++11

g++ -o ./build/server HelloService.cpp server.cpp -I/usr/local/include/thrift -L/usr/local/lib -lthrift -std=c++11

编译成功

运行测试
ctrl+alt+t进入,打开新终端

cd build
./server

运行报错

./server: error while loading shared libraries: libthrift-0.16.0.so: cannot open shared object file: No such file or directory
解决办法

配置/etc/ld.so.conf文件,添加/usr/local/lib

# 查看
cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf

# 添加包含目录
sudo vi /etc/ld.so.conf
include /etc/ld.so.conf.d/*.conf /usr/local/lib

执行修改生效

sudo /sbin/ldconfig

再次运行./server 运行成功,查看一下端口9090是否正常监听

# 使用lsof命令(list open files)
lsof -i
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
server  9565 root    7u  IPv4  93425      0t0  TCP *:9090 (LISTEN)

# 或者使用 netstat -anp # 查看tcp/udp情况

猜你喜欢

转载自blog.csdn.net/youlinhuanyan/article/details/126089104