Running gRPC on C++ in Windows with Bazel in 2023

I did this recently, and found it complicated enough to be worth documenting. Other online examples didn’t quite answer my questions for various reasons – being out of date, not using Bazel, or focused on other platforms/programming languages. Hopefully this saves someone else some effort.

Step 1 – get Bazel

After some trouble with package managers, I simply downloaded the executable from https://github.com/bazelbuild/bazel/releases, saved it to C:\Program Files\Bazel and added that folder to PATH manually.

Step 2 – get gRPC

Download gRPC and put it in a suitable location. I set up a third_party directory for all external dependencies of anything I might be working on in C:\third_party; you might need to change this to match your setup.

Ensure that it builds correctly. I found that the first build would wait indefinitely, but after terminating Bazel (by ctrl+c) and OpenJDK (via Task Manager), the second build works, with a number of warnings:

Step 3 – add gRPC to your project

In your new project, add a local_repository for gRPC to your WORKSPACE file and load it:

local_repository(
    name = "com_github_grpc_grpc",
    path = "C:\\third_party\\grpc",
)

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()

Step 4 – handle internal dependencies

Your proto BUILD files need some imports to work with gRPC:

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
load("@com_github_grpc_grpc//bazel:grpc_build_system.bzl", "grpc_proto_library")

Then you should create proto_library, cc_proto_library and cc_grpc_library rules to export your protos.

Your client and server BUILD rules should depend on these cc_proto_library and cc_grpc_library rules you just configured, as well as gRPC itself. Note that the gRPC dependency comes from the extra local_repository you’ve set up earlier.

Sample code

I’ve adapted the C++ hello world example from gRPC itself to work this way in https://github.com/rgjm/grpc_hello_world.

In the the original gRPC examples, the BUILD files refers to //:grpc++ directly. This works when used within the overall gRPC repository. However, in any other project, you will probably not want to have the full gRPC codebase in your root. You can set up a local repository as I’ve done here, move it into a different subdirectory of your project if you prefer to have everything in the same repository, or use an http_repository which Bazel will automatically download for you.


Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *