节俭生成的代码未定义的参考

Thrift generated code undefined reference

本文关键字:代码 参考 未定义      更新时间:2023-10-16

我正在尝试编写一些代码来使用节俭生成的代码,但我要获得的只是生成代码的错误。我使用的是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

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)

服务器/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})

用于生成的旧约文件: line.thrift

我真的很喜欢这个帮助,因为我已经试图找出解决方案了一段时间。

可能是您周围/传输创建周围有一些混乱(看来您正在使用Transport类型创建套接字)。

您应该尝试在客户端创建以下内容的通信堆栈:

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

设置"主机"answers"端口"值,在您的情况下需要。遵循此创建模式,我成功使用了节俭。