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 the last mile of distributed computing to connect devices, mobile applications, and browsers to backend services.
In this blog post, I would like to a short tutorial on how to use gRPC with C++ and CMake.
Examples
The examples include a hello-world example and an arithmetics example. The implementation and the Docker container are available from the GitHub.
Installation
It is always recommended to use Docker container. The installation process could also be viewed from the Dockerfile.
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.
The data structures of the transmission messages will be defined in proto files.
Implement gRPC Client and Server
The gRPC client and server C++ 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 depend 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.
Implement CMake Build File
To build gRPC programs, we would use find_package(Protobuf) and find_package(gRPC) for our convenience to find necessary building tools and libraries.
For each pair of the client and server programs, the following CMake code snippets are quite reusable for different kind of gRPC programs to generate gRPC source files and application building.
# Include generated *.pb.h files include_directories(${CMAKE_CURRENT_BINARY_DIR})
foreach(target${targets}) add_executable(${target}"${target}.cc"${proto_srcs}${grpc_srcs}) target_link_libraries(${target} PRIVATE ${_REFLECTION}${_GRPC_GRPCPP}${_PROTOBUF_LIBPROTOBUF}) # Cannot do this because these directories are not available during CMake "compile time" # target_include_directories(${proto_hdrs} ${grpc_hdrs}) endforeach()
Demo
In one terminal, we start the gRPC server.
1 2
$ ./bin/arithmetics_server Server listening on 0.0.0.0:50051
In another terminal, we start the gRPC client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ ./bin/arithmetics_client Please enter your binary arithmetic expression: 300 + 200 gRPC returned: 500 Please enter your binary arithmetic expression: 300 - 200 gRPC returned: 100 Please enter your binary arithmetic expression: 300 * 200 gRPC returned: 60000 Please enter your binary arithmetic expression: 300 / 200 gRPC returned: 1 Please enter your binary arithmetic expression: