I just started using Bazel a couple days ago in hopes of something better than CMake. I have a small library that contains only protobuf definitions in its own repository. I've gotten bazel building the proto types and see them in the bazel-bin/proto directory, but am unsure of how to proceed to make this directory an include in dependent workspaces/packages so that I can utilize the output header files?
proto-repo: BUILD
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"],
)
dependent repo (workspace correctly pulls as external and i see proto build output): BUILD
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;
}
build error:
app/bazel-test.cpp:2:10: fatal error: proto/point.pb.h: No such file or directory
2 | #include <proto/point.pb.h>
| ^~~~~~~~~~~~~~~~~~
In general, you should be able to depend on the
cc_proto_library
directly, so the intermediatecc_library
my-protobuf-common
isn't needed generally. The cc toolchain uses-iquote
to add the proto deps, so I believe#include "proto/point.pb.h"
has to be used.proto-repo/WORKSPACE
:proto-repo/BUILD
:proto-repo/proto/point.proto
:main-repo/WORKSPACE
:main-repo/BUILD
:main-repo/main.cc
:Usage: