【问题标题】:Could not find <grpcpp/grpcpp.h>找不到 <grpcpp/grpcpp.h>
【发布时间】:2019-11-14 05:28:09
【问题描述】:

您使用什么版本的 gRPC 以及使用什么语言?

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

什么操作系统(Linux、Windows...)以及哪个版本? Ubuntu 18.04

您使用的是什么运行时/编译器(例如 python 版本或 gcc 版本) 巴泽尔 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 构建服务器:服务器 --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

这里有什么问题?

【问题讨论】:

  • //client:client 的构建规则在哪里?
  • 我的第一个猜测是您需要将grpc 库作为依赖项添加到cc_binary 规则中。类似于"@com_github_grpc_grpc//:grpc++" 的东西。主要的build file 中有许多不同的库。我不知道哪个是正确的,因为我不熟悉grpc。

标签: c++ grpc bazel


【解决方案1】:

问题解决了。我必须将依赖项放在 gRPC 的 deps=[] 中。

终于有人给出了答案。

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

这么愚蠢的错误

【讨论】:

    【解决方案2】:

    您没有安装 grpc 本身(如果您正在构建它,那么可能需要 sudo make install 相当于挡板)

    【讨论】:

    • 你熟悉Bazel吗?你熟悉Bazel如何导入第三方库吗? @fadedreamz
    • @NoviceCai 对不起,我对 Bazel 不熟悉。但我认为它相当于cmake。在 cmake 中它的工作方式是,它拉入 github repo,构建它并将目标添加到当前项目或手动将包含目录添加到项目中。
    • 这正是我问你这个问题的原因。你的假设是不正确的。 Bazel 用于实现与 cmake 相同的目标,以使复杂的编译更容易。但它们与实现这一目标的方式完全不同。导入第三方 Bazel 项目时无需手动安装库。
    【解决方案3】:

    在您的第一个 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 应该会有所帮助。

    【讨论】:

    • hello_world.proto 不是文件吗?
    • 这是一个文件,您在该规则的参数中使用它是正确的。你只是有错误的语法。 bazel build proto:hello_world 告诉你什么?
    猜你喜欢
    • 2022-01-11
    • 2022-08-15
    • 2019-07-23
    • 2021-06-19
    • 1970-01-01
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多