Thrift生成的代码未定义引用

时间:2017-09-08 08:39:17

标签: c++ c++11 cmake thrift

我正在尝试编写一些代码来处理Thrift生成的代码,但我得到的只是生成代码的错误。我正在使用Thrift 0.10.0和g ++ 5.4.1。

错误:

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

目录树:

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

应用/ 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)

服务器/的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文件: line.thrift

我真的很喜欢这方面的帮助,因为我一直试图找出解决方案很长一段时间。

1 个答案:

答案 0 :(得分:0)

您可能对套接字/传输创建感到困惑(似乎您正在创建一个使用TTransport类型的套接字)。

您应该尝试在客户端创建通信堆栈,如下所示:

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);

设置&#34;主持人&#34;和&#34; port&#34;在您的情况下需要的值。 按照这个创建模式,我成功地使用了thrift。