Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence. On the Move.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS


gRPC is a modern open source high performance RPC (Remote Procedure Call) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.

In this blog post, I am going to give a short tutorial on how to use gRPC in C++.


gRPC requires Protobuf, but the latest Protobuf is not supported by the lasted gRPC. We need to install the Protobuf which comes along with the gRPC repository. To make gRPC portable, we also use CMake.

The following bash script installs the latest gRPC, its compatible Protobuf, and the latest CMake.

# gRPC
# https://github.com/grpc/grpc/tree/master/src/cpp
# https://github.com/grpc/grpc/blob/master/BUILDING.md
cd /tmp
apt-get install -y git curl
apt-get install -y build-essential autoconf libtool pkg-config
apt-get install -y libgflags-dev libgtest-dev
apt-get install -y clang libc++-dev
git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc
cd grpc
git submodule update --init
make -j${NPROCS}
# WARNING: After installing with make install there is no easy way to uninstall, which can cause issues if you later want to remove the grpc and/or protobuf installation or upgrade to a newer version.
make install
# Refresh shared library cache.

# Protobuf
# The latest Protobuf might not be compatible with gRPC
# We always install the protobuf included in gRPC
cd /tmp/grpc/third_party/protobuf
make -j${NPROCS}
make install
# Refresh shared library cache.

# CMake
cd /tmp
git clone https://github.com/Kitware/CMake.git
cd CMake
./bootstrap --parallel=${NPROCS}
make -j${NPROCS}
make install


This tutorial assumes you know Protobuf. If you don’t know how to program using Protobuf, please refer to my blog post Google Protocol Buffer Tutorial.

To learn gRPC, the most intuitive way is to go to the gRPC official website and start it official tutorial. However, the official tutorials on the gRPC website are using make instead of CMake to compile. This is not good for program portability. Here I implemented gRPC C++ examples using CMake with the best practice. I also put code comments as many as possible to make sure the code is human readable and easy to understand. Currently CMake only officially support Protobuf but not gRPC. The FindGPRC module for CMake was borrowed. Once CMake officially support gRPC, I will update this repository accordingly.

The source code could be found on my GitHub: gRPC Examples.

The Protobuf proto file, C++ cc file, and CMake CMakeLists.txt files should be self-explanatory since I have put a lot of comments in the code to make it human readable and easy to understand.

Basic Protocol

To summarize the basic protocol of programming using gRPC:

  1. Define the structure of the data which will be used for gRPC transmission in a proto file.
  2. Implement the gRPC C++ client and server programs. The programs will use the gRPC header and source code files of the structured data generated by the Protobuf compiler. We also need Protobuf header and source code because the gRPC header and source code files depends on the Protobuf header and source code. Therefore, the good protocol is, generate Protobuf header and source code using the proto file and the Protobuf Compiler, generate gRPC header and source code using the proto file and the Protobuf Compiler (gRPC plugin), build client/server gRPC library using Protobuf header and source code and gRPC header and source code and linked with gRPC libraries, linked the library to the client and the server program.
  3. Write CMake compile system to generate the header and source code files for gRPC, static libraries of gRPC program, and the executable files.