0

I'm trying to write some code to work with Thrift-generated code but all I'm getting is a error on generated code. I'm using Thrift 0.10.0 and g++ 5.4.1.

Error:

server/libserver.a(TalkService.cpp.o): In function `std::less<lineserver::Contact>::operator()(lineserver::Contact const&, lineserver::Contact const&) const':
TalkService.cpp:(.text._ZNKSt4lessIN10lineserver7ContactEEclERKS1_S4_[_ZNKSt4lessIN10lineserver7ContactEEclERKS1_S4_]+0x23): undefined reference to `lineserver::Contact::operator<(lineserver::Contact const&) const'
collect2: error: ld returned 1 exit status
CMakeFiles/testshit.dir/build.make:99: recipe for target 'testshit' failed
make[2]: *** [testshit] Error 1
CMakeFiles/Makefile2:68: recipe for target 'CMakeFiles/testshit.dir/all' failed
make[1]: *** [CMakeFiles/testshit.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Directory tree:

├── app
│   └── main.cpp
├── CMakeLists.txt
└── server
    ├── CMakeLists.txt
    ├── line_constants.cpp
    ├── line_constants.h
    ├── line_types.cpp
    ├── line_types.h
    ├── TalkService.cpp
    └── TalkService.h

app/main.cpp:

#include <iostream>
#include <thrift/transport/THttpClient.h>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/transport/TTransportUtils.h>
#include <boost/algorithm/string.hpp>
#include "../server/TalkService.h"

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace lineserver;

int main()
{
    boost::shared_ptr<TTransport> socket(new THttpClient("https://gd2.line.naver.jp", 443, "/api/v3/TalkService.do"));
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    TalkServiceClient client(protocol);

    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)
project(testshit)

set(CMAKE_CXX_STANDARD 11)

find_library(THRIFT thrift)

include_directories(server app)
link_directories(server app)

add_subdirectory(server)

set(SOURCE_FILES app/main.cpp)

add_executable(testshit ${SOURCE_FILES})
target_link_libraries(testshit ${THRIFT} server)

server/CMakeLists.txt:

find_library(THRIFT thrift)

file(GLOB SRC_FILES line_constants.cpp line_types.cpp TalkService.cpp)
add_library(server ${SRC_FILES})

target_link_libraries(server ${THRIFT})

Thrift file used for generating: line.thrift

I'd really love some help with this because I've been trying to figure out the solution for quite some time.

1 Answers1

0

May be you have some confusion around socket/transport creation (it seems you're creating a socket with the TTransport type).

You should try to create the communication stack as follows on the client side :

boost::shared_ptr<TSocket> socket(new TSocket("host", "port"));

boost::shared_ptr<TTransport> transport = boost::shared_ptr<TTransport>(new THttpClient("https://gd2.line.naver.jp", 443, "/api/v3/TalkService.do"));

boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));

TalkServiceClient client(protocol);

Set "host" and "port" values as needed in your case. Following this creation pattern I successfully used thrift.

Theforgotten
  • 140
  • 7
  • I made the changes but I don't think the error is being caused by main.cpp because it is still the same even after making the changes – highafdoge Sep 08 '17 at 17:33
  • Could you provide the thrift generated output files ? It seems your Contact class/struct is missing the operator< definition, needed by (maybe) an STL container. – Theforgotten Sep 11 '17 at 06:36