fatal error: 'grpcpp/grpcpp.h' file not found C++

fatal error: 'grpcpp/grpcpp.h' file not found C++

上下文

在过去的 5-6 个月里,我一直在使用 C++,并且我开始学习 gRPC。我已经按照许多在线教程开始使用,但我想从头开始构建一个客户端-服务器通信应用程序。可能有点太多了,但我正在尽我最大的努力去理解如何从头开始工作,而不是下载、输入 'make',然后得到一个我不知道如何工作的产品实施到我自己的项目中。

目标:创建并运行一个简单的 C++ gRPC 客户端-服务器通信

版本

使用 VSCode IDE.

文件

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

编译步骤

  1. 使用 mathtest.proto 通过 'protoc'(或 protobuf)创建必要的文件,方法是执行以下命令:protoc --grpc_out=. --plugin=protoc-gen-grpc=/opt/homebrew/bin/grpc_cpp_plugin mathtest.proto & protoc --cpp_out=. mathtest.proto

这将创建以下文件:

  1. 使用这些命令编译 client.cpp 和 server.cpp 文件以创建可执行二进制文件:g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client 'pkg-config --libs protobuf grpc++'(注意:在这个 post 中,我在命令中使用单引号行,但在实际命令中我使用了反引号;只是想说清楚)

错误

您可能已经注意到,我无法开始编译服务器,因为我无法先通过客户端编译。在编译的第 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 returns 这个:

/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 中,它们也包含必要的文件...

我错过了什么??

将编译命令更改为

g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs --cflags protobuf grpc++`

--cflags 位要求 pkg-config 吐出设置 header 搜索路径的必要参数(在我的系统 -I/opt/homebrew/Cellar/grpc/1.41.1/include 和其他系统上)