找不到<grpcpp / grpcpp.h>

时间:2019-11-14 05:28:09

标签: c++ grpc bazel

您使用的是什么版本的gRPC和使用什么语言?

name = "com_github_grpc_grpc"
remote = "https://github.com/grpc/grpc"
commit = "bd0aa9a600a13cc988c6ebfd12deab8d1abcf171"

什么操作系统(Linux,Windows等)和哪个版本? Ubuntu 18.04

您使用的是什么运行时/编译器(例如python版本或gcc版本) bazel 1.0.1

您做了什么? 编写了一个小的grpc测试应用程序

在工作空间中

    workspace(name = "grpc_test")

    load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

    git_repository(
        name = "com_github_grpc_grpc",
        remote = "https://github.com/grpc/grpc",
        commit = "bd0aa9a600a13cc988c6ebfd12deab8d1abcf171",
    )

    load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
    grpc_deps()

    load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
    grpc_extra_deps()

在hello_world.proto


    syntax = "proto3";

    package hello_world;

    // The greeting service definition.
    service Greeter {
        // Sends a greeting
        rpc SayHello (HelloRequest) returns (HelloReply) {}
    }

    // The request message containing the user's name.
    message HelloRequest {
        string name = 1;
    }

    // The response message containing the greetings
    message HelloReply {
        string message = 1;
    }

在hello_world.proto构建中


    package(default_visibility = ["//visibility:public"])

    load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")

    cc_grpc_library(
        name = "hello_world",
        srcs = [":hello_world.proto"],
        deps = [],
    )

在server.cpp中


    #include
    #include
    #include

    #include <grpc++/grpc++.h>

    #include "proto/hello_world.pb.h"
    #include "proto/hello_world.grpc.pb.h"

    using grpc::Server;
    using grpc::ServerBuilder;
    using grpc::ServerContext;
    using grpc::Status;
    using hello_world::HelloRequest;
    using hello_world::HelloReply;
    using hello_world::Greeter;
    using std::string;

    class GreeterServiceImpl final : public Greeter::Service {
        Status SayHello(
          ServerContext* context, 
          const HelloRequest* request,
          HelloReply* reply) override {
            string prefix("Hello ");
            reply->set_message(prefix + request->name());
            return Status::OK;
        }
    };

    void RunServer() {
        std::string server_address("0.0.0.0:10001");
        GreeterServiceImpl service;

        ServerBuilder builder;
        builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
        builder.RegisterService(&service);
        std::unique_ptr server(builder.BuildAndStart());
        std::cout << "Server listening on " << server_address << std::endl;

        server->Wait();
    }

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

        return 0;
    }

在server.cpp中构建


    package(default_visibility = ["//visibility:public"])

    cc_binary(
        name = "server",
        srcs = ["server.cpp"],
        deps = [
            "//proto:hello_world",
        ],
    )

您期望看到什么? 我应该可以跑步 bazel构建服务器:server --sandbox_debug

您看到了什么?


    INFO: Writing tracer profile to '/home/caixuanting/.cache/bazel/_bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/command.profile.gz'
    INFO: Analyzed target //client:client (0 packages loaded, 0 targets configured).
    INFO: Found 1 target...
    ERROR: /home/caixuanting/grpc_test/client/BUILD:3:1: C++ compilation of rule '//client:client' failed (Exit 1) linux-sandbox failed: error executing command
    (cd /home/caixuanting/.cache/bazel/bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test &&
    exec env -
    PATH=/home/caixuanting/.vscode-server/bin/86405ea23e3937316009fc27c9361deee66ffbf5/bin:/home/caixuanting/.vscode-server/bin/86405ea23e3937316009fc27c9361deee66ffbf5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
    PWD=/proc/self/cwd
    TMPDIR=/tmp
    /home/caixuanting/.cache/bazel/bazel_caixuanting/install/0eaad79e03c843a15f078930a471406a/embedded_binaries/linux-sandbox -t 15 -w /home/caixuanting/.cache/bazel/bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test -w /tmp -w /dev/shm -D -- /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF bazel-out/k8-fastbuild/bin/client/objs/client/client.pic.d '-frandom-seed=bazel-out/k8-fastbuild/bin/client/objs/client/client.pic.o' -fPIC -iquote . -iquote bazel-out/k8-fastbuild/bin -iquote external/com_google_protobuf -iquote bazel-out/k8-fastbuild/bin/external/com_google_protobuf -iquote external/zlib -iquote bazel-out/k8-fastbuild/bin/external/zlib -iquote external/com_github_grpc_grpc -iquote bazel-out/k8-fastbuild/bin/external/com_github_grpc_grpc -iquote external/bazel_tools -iquote bazel-out/k8-fastbuild/bin/external/bazel_tools -isystem external/com_google_protobuf/src -isystem bazel-out/k8-fastbuild/bin/external/com_google_protobuf/src -isystem external/zlib -isystem bazel-out/k8-fastbuild/bin/external/zlib -isystem external/com_github_grpc_grpc/include -isystem bazel-out/k8-fastbuild/bin/external/com_github_grpc_grpc/include -isystem external/com_github_grpc_grpc/src/core/ext/upb-generated -isystem bazel-out/k8-fastbuild/bin/external/com_github_grpc_grpc/src/core/ext/upb-generated -fno-canonical-system-headers -Wno-builtin-macro-redefined '-D__DATE="redacted"' '-D__TIMESTAMP="redacted"' '-D__TIME="redacted"' -c client/client.cpp -o bazel-out/k8-fastbuild/bin/client/_objs/client/client.pic.o)
    src/main/tools/linux-sandbox.cc:154: linux-sandbox-pid1 has PID 4821
    src/main/tools/linux-sandbox-pid1.cc:175: working dir: /home/caixuanting/.cache/bazel/_bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test
    src/main/tools/linux-sandbox-pid1.cc:194: writable: /home/caixuanting/.cache/bazel/_bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test
    src/main/tools/linux-sandbox-pid1.cc:194: writable: /tmp
    src/main/tools/linux-sandbox-pid1.cc:194: writable: /dev/shm
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /dev
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /dev/pts
    src/main/tools/linux-sandbox-pid1.cc:265: remount rw: /dev/shm
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /dev/mqueue
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /dev/hugepages
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /run
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /run/lock
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /run/snapd/ns
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /run/user/1000
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/kernel/security
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/unified
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/systemd
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/freezer
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/blkio
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/perf_event
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/pids
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/hugetlb
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/net_cls,net_prio
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/memory
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/cpuset
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/cpu,cpuacct
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/rdma
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/cgroup/devices
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/pstore
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/kernel/debug
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/kernel/config
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /sys/fs/fuse/connections
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /proc
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /proc/sys/fs/binfmt_misc
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /proc/sys/fs/binfmt_misc
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /snap/docker/384
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /snap/core/7917
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /snap/core/8039
    src/main/tools/linux-sandbox-pid1.cc:265: remount ro: /var/lib/lxcfs
    src/main/tools/linux-sandbox-pid1.cc:265: remount rw: /home/caixuanting/.cache/bazel/_bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test
    src/main/tools/linux-sandbox-pid1.cc:265: remount rw: /home/caixuanting/.cache/bazel/_bazel_caixuanting/56e1e1f496eedd6c32ee33d10eb39038/sandbox/linux-sandbox/8/execroot/grpc_test
    src/main/tools/linux-sandbox-pid1.cc:265: remount rw: /tmp
    src/main/tools/linux-sandbox-pid1.cc:265: remount rw: /dev/shm
    src/main/tools/process-tools.cc:118: sigaction(32, &sa, nullptr) failed
    src/main/tools/process-tools.cc:118: sigaction(33, &sa, nullptr) failed
    client/client.cpp:5:10: fatal error: grpcpp/grpcpp.h: No such file or directory
    #include <grpcpp/grpcpp.h>
    ^~~~~~~~~~~~~~~~~
    compilation terminated.
    src/main/tools/linux-sandbox-pid1.cc:437: waitpid returned 2
    src/main/tools/linux-sandbox-pid1.cc:457: child exited with code 1
    src/main/tools/linux-sandbox.cc:204: child exited normally with exitcode 1
    Target //client:client failed to build
    Use --verbose_failures to see the command lines of failed build steps.
    INFO: Elapsed time: 1.559s, Critical Path: 0.09s
    INFO: 0 processes.
    FAILED: Build did NOT complete successfully

这是怎么了?

3 个答案:

答案 0 :(得分:1)

问题已解决。对于gRPC,我必须将依赖项放在deps = []中。

最后有人给出答案。

我需要在部门中添加“ @com_github_grpc_grpc // :: grpc ++”。

如此愚蠢的错误

答案 1 :(得分:0)

您没有安装grpc本身(如果要构建它,则可能需要sudo make install等价的挡板)

答案 2 :(得分:0)

在您的第一个BUILD文件中,您的cc_grpc_library规则应为

    cc_grpc_library(
        name = "hello_world",
        srcs = ["relative/path/to/hello_world.proto"],
        deps = [],
    )

请注意,srcs参数中没有冒号。

您的srcs参数以冒号开头,这是用于传递给deps的本地规则依赖项的Bazel语法。对于src自变量,请在每个源文件中使用无冒号的相对文件路径。 bazel example应该会有所帮助。