简体   繁体   中英

QT qmake GRPC implementation

I want to implement GRPC on my qt project but first I try run a grpc example but I get undefined reference error.

main.cpp:

#include <helloworld.grpc.pb.h>

#include <grpc/grpc.h>
#include <grpcpp/server_builder.h>

#include <iostream>

class GreeterService final : public helloworld::Greeter::Service {
public:
  virtual ::grpc::Status SayHello(::grpc::ServerContext* context, const ::helloworld::HelloRequest* request, ::helloworld::HelloReply* response) {
    std::cout << "Server: message for \"" << request->name() << "\"." << std::endl;

    response->set_message("Hi " + request->name());

    return grpc::Status::OK;
  }
};

int main(int argc, char* argv[]) {
  grpc::ServerBuilder builder;
  builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials());

  GreeterService my_service;
  builder.RegisterService(&my_service);

  std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
  server->Wait();

  return 0;
}

.pro file:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \

INCLUDEPATH += /home/grpc/include \
               /home/grpc/
DEPENDPATH  += /home/grpc//include \
               /home/grpc/
QMAKE_LFLAGS += -Wl,-rpath,"path_to_libgrpc++.so.1.45"
LIBS += -L/usr/local/lib `pkg-config --libs protobuf grpc++` -L/usr/lib -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu


# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

Error:

undefined reference to `helloworld::Greeter::Service::Service()'

I don't know why I get this error, compailer can see headers main also can see. But I think there are missing things in my.pro file

The error you are seeing is likely due to the fact that you are not linking against the gRPC library correctly.

To fix this, you need to make sure that you are linking against the gRPC library in your project file (.pro). You can do this by adding the following lines to your.pro file:

LIBS += -lgrpc++

This will tell the linker to link against the gRPC C++ library, which includes the helloworld::Greeter::Service class that you are trying to use.

You may also need to specify the path to the library if it is not in a standard library search path. You can do this by adding the following line to your.pro file:

LIBS += -L/path/to/libgrpc++.so

Replace /path/to/libgrpc++.so with the actual path to the libgrpc++.so library on your system.

Once you have added these lines to your.pro file, try rebuilding your project. This should resolve the undefined reference error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM