I get an errors with thrift TThreadedServer server building.
Build log:
[main] Building folder: Thrift_client_server
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build "/home/konstantin/c++ projects/Thrift_client_server/build" --config Debug --target all -j 14 --
[build] Consolidate compiler generated dependencies of target server
[build] [ 50%] Building CXX object CMakeFiles/server.dir/server.cpp.o
[build] [100%] Linking CXX executable server
[build] /usr/bin/ld: CMakeFiles/server.dir/server.cpp.o: warning: relocation against `_ZTV23MessageProcessorFactory' in read-only section `.text._ZN23MessageProcessorFactoryC2ERKSt10shared_ptrI16MessageIfFactoryE[_ZN23MessageProcessorFactoryC5ERKSt10shared_ptrI16MessageIfFactoryE]'
[build] /usr/bin/ld: CMakeFiles/server.dir/server.cpp.o: in function `MessageProcessorFactory::MessageProcessorFactory(std::shared_ptr<MessageIfFactory> const&)':
[build] /home/konstantin/c++ projects/Thrift_client_server/api/gen-cpp/Message.h:209: undefined reference to `vtable for MessageProcessorFactory'
[build] /usr/bin/ld: warning: creating DT_TEXTREL in a PIE
[build] collect2: error: ld returned 1 exit status
[build] gmake[2]: *** [CMakeFiles/server.dir/build.make:98: server] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/server.dir/all] Error 2
[build] gmake: *** [Makefile:91: all] Error 2
[proc] The command: /usr/bin/cmake --build "/home/konstantin/c++ projects/Thrift_client_server/build" --config Debug --target all -j 14 -- exited with code: 2
[driver] Build completed: 00:00:01.631
[build] Build finished with exit code 2
If i remove MessageProcessorFactory pointer (Message is a name of thrift service), all errors disappear. What's a problem?
CMakeLists.txt:
cmake_minimum_required(VERSION 3.11)
project(server)
set(THRIFT_LIB_DIR "/home/konstantin/thrift/thrift-0.19.0")
find_library(THRIFT_LIB thrift ${THRIFT_LIB_DIR})
add_executable(${PROJECT_NAME} server.cpp)
target_link_libraries(${PROJECT_NAME} ${THRIFT_LIB})
Server's code:
#include <thrift/server/TThreadedServer.h>
#include <thrift/transport/TServerSocket.h>
#include <thrift/transport/TTransport.h>
#include <thrift/TProcessor.h>
#include <thrift/transport/TBufferTransports.h>
#include <memory>
#include <string>
#include "api/gen-cpp/Message.h"
using namespace ::apache::thrift::server;
using namespace ::apache::thrift::protocol;
using namespace ::apache::thrift::transport;
using namespace ::apache::thrift;
class MessageHandler : public MessageIf
{
public:
virtual void mes(std::string &_return) override
{
_return = "Hello from the server";
}
virtual ~MessageHandler(){}
};
class MessageHandlerFactory : public MessageIfFactory
{
public:
virtual MessageIf *getHandler(const ::apache::thrift::TConnectionInfo &connInfo)
{
return new MessageHandler();
}
virtual void releaseHandler(MessageIf *handler)
{
delete handler;
}
virtual ~MessageHandlerFactory(){}
};
int main(){
std::shared_ptr<MessageIfFactory> handler_factory = std::make_shared<MessageHandlerFactory>();
auto processor_factory = std::make_shared<MessageProcessorFactory>(handler_factory);
auto transport_socket = std::make_shared<TServerSocket>(9090);
auto transport_factory = std::make_shared<TFramedTransportFactory>();
auto protocol_factory = std::make_shared<TBinaryProtocolFactory>();
TThreadedServer server(processor_factory, transport_socket, transport_factory, protocol_factory);
server.serve();
return 0;
}