编译具有错误的grpc服务器:体系结构x86_64的未定义符号

时间:2019-04-28 07:26:04

标签: c++ server grpc

我正在为自己做一个家庭项目,为了学习新技术和学习语言,我决定尝试制作一个grpc服务器 我本人已经很长时间没有练习语言了,所以我有些困难 描述protobuf应用程序(service.proto)的结构:

syntax = "proto3";

package session;

message SaveRequest {
    string sessid = 1;
    int64 userId = 2;
    string access_token = 3;
    string refresh_token = 4;
}

message SaveResponse {
    bool success = 1;
}

message GetRequest {
    string sessid = 1;
}

message GetResponse {
    int64 userId = 1;
    string access_token = 2;
    string refresh_token = 3;
}

service SessionService {
    rpc Save (SaveRequest) returns (SaveResponse) {}
    rpc Get (GetRequest) returns (GetResponse) {}
}

Makefile:

build:
    rm -f ./bin/*.o \
    && g++ \
        -std=c++14 \
        -stdlib=libc++ \
        -O3 \
        -I ~/cpp/grpc/src/cpp/server \
        -I ~/cpp/grpc/third_party/protobuf/src \
        -I ~/cpp/grpc/third_party/libcxx/src \
        -Wall \
        -v \
        -dynamiclib \
        ./server.cc \
        -o ./bin/server.o \
    && g++ ./bin/server.o -o ./bin/server

proto.grpc:
    protoc -I ./proto/ --grpc_out=. --plugin=protoc-gen-grpc=~/cpp/grpc/bins/opt/grpc_cpp_plugin ./proto/service.proto

proto.message:
    protoc -I ./proto --cpp_out=. ./proto/service.proto

描述服务器server.cc的代码:

/**
 * Service Session
 */

#include <iostream>
#include <memory>
#include <string>

#include <grpcpp/grpcpp.h>

#include "service.grpc.pb.h"

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;

using session::SaveRequest;
using session::SaveResponse;
using session::GetRequest;
using session::GetResponse;
using session::SessionService;

class ServiceSessionServer final : public SessionService::Service {
public:
    Status Save(ServerContext* context, const SaveRequest* request, SaveResponse* response) override {
        // implement me
        return Status(::grpc::StatusCode::UNIMPLEMENTED, "");
    }

    Status Get(ServerContext* context, const GetRequest* request, GetResponse* response) override {
        // implement me
        return Status(::grpc::StatusCode::UNIMPLEMENTED, "");
    }
};

void RunServer() {
    std::string server_address("0.0.0.0:50051");
    ServiceSessionServer service;

    ServerBuilder builder;
    // Listen on the given address without any authentication mechanism.
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    // Register "service" as the instance through which we'll communicate with
    // clients. In this case it corresponds to an *synchronous* service.
    builder.RegisterService(&service);
    // Finally assemble the server.
    std::unique_ptr<Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;

    // Wait for the server to shutdown. Note that some other thread must be
    // responsible for shutting down the server for this call to ever return.
    server->Wait();
}

int main (int argc, char** argv) {
    RunServer();

    return 0;
}

但是代码编译失败:

 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -macosx_version_min 10.13.0 -o ./bin/server.o /var/folders/lq/1_ktp_d138z51ff97qzdpknh0000gp/T/server-ccf1e3.o -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/10.0.0/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "session::SessionService::Service::Service()", referenced from:
      RunServer() in server-ccf1e3.o
  "session::SessionService::Service::~Service()", referenced from:
      RunServer() in server-ccf1e3.o
      ServiceSessionServer::~ServiceSessionServer() in server-ccf1e3.o
      ServiceSessionServer::~ServiceSessionServer() in server-ccf1e3.o
  "grpc_impl::ServerBuilder::BuildAndStart()", referenced from:
      RunServer() in server-ccf1e3.o
  "grpc_impl::ServerBuilder::RegisterService(grpc::Service*)", referenced from:
      RunServer() in server-ccf1e3.o
  "grpc_impl::ServerBuilder::AddListeningPort(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<grpc_impl::ServerCredentials>, int*)", referenced from:
      RunServer() in server-ccf1e3.o
  "grpc_impl::ServerBuilder::ServerBuilder()", referenced from:
      RunServer() in server-ccf1e3.o
  "grpc_impl::ServerBuilder::~ServerBuilder()", referenced from:
      RunServer() in server-ccf1e3.o
  "grpc_impl::InsecureServerCredentials()", referenced from:
      RunServer() in server-ccf1e3.o
  "typeinfo for session::SessionService::Service", referenced from:
      typeinfo for ServiceSessionServer in server-ccf1e3.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [build] Error 1

0 个答案:

没有答案