Thrift installation and problems encountered under ubuntu16.04

Install thrift under ubuntu

ubuntu 16.04
thrift 0.16.0

boost install

method one

#安装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

after decompression

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

Download boost
https://sourceforge.net/projects/boost/files/latest/download

Method Two

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

After the installation is completed, the default installation directory is /usr/include/boost

test

Create a 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;
}

compile

g++ -o stest main.cpp

implement

./stest
hi1

success

install thrift

thrfit official website, click here

Download thrfit source package link

Unzip after downloading

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

Install

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

During the compilation process, an error is reported

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

According to the installation method under the debian system , some dependencies need to be installed

# 依赖安装
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

install thrift

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

test demo

1. Define the interface filehello.thrift

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

2. Compile and generate cpp interface

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

insert image description here
Openserver.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;
}

compileserver.cpp

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

There is an errorThis 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;

Prompt to add options-std=c++11

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

compile successfully

Run the test
ctrl+alt+tand enter, open a new terminal

cd build
./server

run error

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

config /etc/ld.so.conffile, add/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

Execute the modification to take effect

sudo /sbin/ldconfig

Run again ./serverand run successfully, check if port 9090 is listening normally

# 使用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情况

Guess you like

Origin blog.csdn.net/youlinhuanyan/article/details/126089104