Skip to content

Commit 63bcfa0

Browse files
Binit SHahhello-binit
Binit SHah
authored andcommitted
Add instructions on using the BCR to the README.md
If your project is using Bazel, you can make your builds more reproducible by depending on protobuf through the Bazel Central Registry, instead of building it from source.
1 parent 5b0fa1d commit 63bcfa0

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,53 @@ CMake Installation
1111
To compile or install protobuf from source using CMake, see
1212
[cmake/README.md](../cmake/README.md).
1313

14+
C++ Protobuf using the Bazel Central Registry - Unix
15+
----------------------------------------------------
16+
17+
The C++ Protocol Buffer runtime is available on the [Bazel Central Registry](https://registry.bazel.build/modules/protobuf). If your project uses Bazel, you can simply add `@protobuf//:protobuf` as a dependency. Here's an example setup:
18+
19+
* main.cpp
20+
* ```c
21+
#include "<messages>.pb.h"
22+
int main() {
23+
GOOGLE_PROTOBUF_VERIFY_VERSION;
24+
return 0;
25+
}
26+
```
27+
* \<messages\>.pb.cc/.h
28+
* Generated using `protoc`, downloaded from [Github Releases](https://github.com/protocolbuffers/protobuf/releases).
29+
* Check the runtime version of the compiler using `protoc --version`. It'll return something like "libprotoc 29.2".
30+
* MODULE.bazel
31+
* ```
32+
bazel_dep(name = "protobuf", version = "29.2")
33+
```
34+
* Note that the version matches `protoc`'s version
35+
* BUILD
36+
* ```
37+
cc_library(
38+
name = "<messages>",
39+
srcs = ["<messages>.pb.cc"],
40+
hdrs = ["<messages>.pb.h"],
41+
deps = [
42+
"@protobuf//:protobuf",
43+
],
44+
)
45+
cc_binary(
46+
name = "main",
47+
srcs = ["main.cpp"],
48+
deps = [
49+
":<messages>",
50+
],
51+
)
52+
```
53+
* WORKSPACE
54+
* ```
55+
workspace(name = "example")
56+
```
57+
58+
Then build it using `bazel build :main` and run the binary using `./bazel-bin/main`
59+
60+
1461
C++ Protobuf - Unix
1562
-----------------------
1663

0 commit comments

Comments
 (0)