gpt4 book ai didi

protocol-buffers - 如何在不同的工作区/包中引用 bazel c++ protobuf 输出 header

转载 作者:行者123 更新时间:2023-12-03 08:06:39 25 4
gpt4 key购买 nike

我几天前才开始使用 Bazel,希望能得到比 CMake 更好的东西。我有一个小型库,它自己的存储库中仅包含 protobuf 定义。我已经让 bazel 构建了原型(prototype)类型并在 bazel-bin/proto 目录中看到它们,但不确定如何继续将该目录包含在依赖工作区/包中,以便我可以利用输出头文件?

原始存储库:构建

load("@rules_cc//cc:defs.bzl", "cc_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

cc_library(
name = "my-protobuf-common",
hdrs = [
":my-proto-lib",
],
copts = ["--std=c++17"],
includes = [
":my-proto-lib",
],
linkstatic = True,
visibility = ["//visibility:public"],
deps = [":my-proto-lib"],
)

cc_proto_library(
name = "my-proto-lib",
visibility = ["//visibility:public"],
deps = [":my-proto"],
)

proto_library(
name = "my-proto",
srcs = [
"proto/point.proto",
"proto/point-geodetic.proto",
"proto/point-ned.proto",
],
visibility = ["//visibility:public"],
)

依赖的存储库(工作区正确地作为外部拉取,我看到原始构建输出):构建

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
name = "my-service",
srcs = [
"app/bazel-test.cpp",
],
hdrs = [
"@mpc//:my-protobuf-common",
],
copts = ["--std=c++17"],
deps = [
"@mpc//:my-protobuf-common",
],
)

bazel-test.cpp

#include <iostream>
#include <proto/point.pb.h>

int main() {
MyProtobufCommon::Point p;
}

构建错误:

app/bazel-test.cpp:2:10: fatal error: proto/point.pb.h: No such file or directory
2 | #include <proto/point.pb.h>
| ^~~~~~~~~~~~~~~~~~

最佳答案

一般来说,您应该能够直接依赖 cc_proto_library,因此中间的 cc_library my-protobuf-common 不是一般需要。 cc 工具链使用 -iquote 添加 proto deps,因此我相信必须使用 #include "proto/point.pb.h"

proto-repo/WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_proto",
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
strip_prefix = "rules_proto-4.0.0",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

proto-repo/BUILD:

cc_proto_library(
name = "point_cc_proto",
deps = [":point"],
visibility = ["//visibility:public"],
)

proto_library(
name = "point",
srcs = ["proto/point.proto"],
)

proto-repo/proto/point.proto:

syntax = "proto3";

package my_protos.point;

message Point {
optional int32 x = 1;
optional int32 y = 2;
}

主存储库/WORKSPACE:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

local_repository(
name = "my_protos",
path = "../proto-repo",
)

http_archive(
name = "rules_proto",
sha256 = "66bfdf8782796239d3875d37e7de19b1d94301e8972b3cbd2446b332429b4df1",
strip_prefix = "rules_proto-4.0.0",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
"https://github.com/bazelbuild/rules_proto/archive/refs/tags/4.0.0.tar.gz",
],
)
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
rules_proto_dependencies()
rules_proto_toolchains()

main-repo/BUILD:

cc_binary(
name = "main",
srcs = ["main.cc"],
deps = ["@my_protos//:point_cc_proto"],
)

main-repo/main.cc:

#include <iostream>
#include "proto/point.pb.h"

int main() {

my_protos::point::Point p;
p.set_x(123);
p.set_y(456);
std::cout << p.DebugString();

return 0;
}

用法:

main-repo$ bazel run main
INFO: Analyzed target //:main (43 packages loaded, 570 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
bazel-bin/main
INFO: Elapsed time: 6.166s, Critical Path: 5.33s
INFO: 106 processes: 4 internal, 102 linux-sandbox.
INFO: Build completed successfully, 106 total actions
INFO: Build completed successfully, 106 total actions
x: 123
y: 456

关于protocol-buffers - 如何在不同的工作区/包中引用 bazel c++ protobuf 输出 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72148865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com