【发布时间】:2022-01-11 06:46:08
【问题描述】:
上下文
在过去的 5-6 个月里,我一直在使用 C++,并且开始学习 gRPC。我已经在线学习了许多教程以开始使用,但我想从头开始构建一个客户端-服务器通信应用程序。可能有点太多了,但我正在尽我最大的努力去了解如何让这一切从头开始工作,而不是下载、输入“make”,然后拥有一个我不知道如何实现的工作产品我自己的项目。
目标:创建并运行简单的 C++ gRPC 客户端-服务器通信
版本
我正在为我的 IDE 使用 Vs Code。
-
协议 = libprotoc 3.17.3
-
gRPC = 1.41.1
-
make = 3.81
文件
mathtest.proto
syntax = "proto3";
option java_package = "ex.grpc";
package mathtest;
// Defines the service
service MathTest {
// Function invoked to send the request
rpc sendRequest (MathRequest) returns (MathReply) {}
}
// The request message containing requested numbers
message MathRequest {
int32 a = 1;
int32 b = 2;
}
// The response message containing response
message MathReply {
int32 result = 1;
}
server.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathServiceImplementation final : public MathTest::Service {
Status sendRequest(
ServerContext* context,
const MathRequest* request,
MathReply* reply
) override {
int a = request->a();
int b = request->b();
reply->set_result(a * b);
return Status::OK;
}
};
void Run() {
std::string address("0.0.0.0:5000");
MathServiceImplementation service;
ServerBuilder builder;
builder.AddListeningPort(address, grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on port: " << address << std::endl;
server->Wait();
}
int main(int argc, char** argv) {
Run();
return 0;
}
client.cpp
#include <string>
#include <grpcpp/grpcpp.h>
#include "mathtest.grpc.pb.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using mathtest::MathTest;
using mathtest::MathRequest;
using mathtest::MathReply;
class MathTestClient {
public:
MathTestClient(std::shared_ptr<Channel> channel) : stub_(MathTest::NewStub(channel)) {}
int sendRequest(int a, int b) {
MathRequest request;
request.set_a(a);
request.set_b(b);
MathReply reply;
ClientContext context;
Status status = stub_->sendRequest(&context, request, &reply);
if(status.ok()){
return reply.result();
} else {
std::cout << status.error_code() << ": " << status.error_message() << std::endl;
return -1;
}
}
private:
std::unique_ptr<MathTest::Stub> stub_;
};
void Run() {
std::string address("0.0.0.0:5000");
MathTestClient client(
grpc::CreateChannel(
address,
grpc::InsecureChannelCredentials()
)
);
int response;
int a = 5;
int b = 10;
response = client.sendRequest(a, b);
std::cout << "Answer received: " << a << " * " << b << " = " << response << std::endl;
}
int main(int argc, char* argv[]){
Run();
return 0;
}
编译步骤
- 使用
mathtest.proto通过'protoc'(或protobuf)创建必要的文件,方法是执行这些:protoc --grpc_out=. --plugin=protoc-gen-grpc=/opt/homebrew/bin/grpc_cpp_plugin mathtest.proto&protoc --cpp_out=. mathtest.proto
这将创建以下文件:
mathtest.pb.hmathtest.pb.ccmathtest.grpc.pb.hmathtest.grpc.pb.cc
- 使用以下命令编译 client.cpp 和 server.cpp 文件以创建可执行二进制文件:
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client 'pkg-config --libs protobuf grpc++'(注意:在这篇文章中,我在命令行中使用单引号,但在实际命令中我使用反引号;只是想说明一下)
错误
您可能会注意到,我无法编译服务器,因为我无法先通过客户端编译。在编译的第 2 步执行上述命令后,这是我的输出:
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs protobuf grpc++`
client.cpp:4:10: fatal error: 'grpcpp/grpcpp.h' file not found
#include <grpcpp/grpcpp.h>
^~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.pb.cc:4:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
In file included from mathtest.grpc.pb.cc:5:
./mathtest.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [client] Error 1
这是我真正的困惑......
C++ 智能感知在查找这些文件时没有问题。我的 $PATH 变量指向这些文件夹,我的 VS Code 包含路径也指向这些文件夹。我不确定我哪里出错了......
echo $PATH 返回:
/opt/homebrew/bin:/opt/homebrew/sbin:/opt/homebrew/include:/opt/homebrew/Cellar:/opt/homebrew/opt/libtool/libexec/gnubin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/tzeller/.local/bin
有问题的文件夹(“google”和“grcpp”)位于/opt/homebrew/include 中,它们也包含必要的文件...
我错过了什么??
【问题讨论】:
-
$PATH 与预处理器包含路径无关。
标签: c++ g++ protocol-buffers grpc protoc