Introduction to gRPC for C++
gRPC is a modern Remote Procedure Call (RPC) framework developed by Google. It uses Protocol Buffers (protobuf) for data serialization and HTTP/2 as the transport protocol. For the C++ language, gRPC provides native libraries that allow you to build high-performance microservices and distributed systems.
In this article, we will cover the main libraries, environment setup, and write a complete client-server application example in C++.
Main gRPC Libraries for C++
To work with gRPC in C++, you need to install several components:
- grpc — the core library containing server and client implementations.
- grpc++ — a C++ wrapper over gRPC providing a convenient API.
- protobuf — a library for working with Protocol Buffers (class generation, serialization/deserialization).
- grpc_cpp_plugin — a plugin for the protoc compiler that generates C++ code from .proto files.
Dependencies may also be required: abseil-cpp, openssl, zlib, and c-ares.
Installing gRPC and protobuf
The recommended method is building from source using CMake. Example for Linux/macOS:
git clone --recurse-submodules -b v1.60.0 https://github.com/grpc/grpccd grpcmkdir build && cd buildcmake .. -DgRPC_INSTALL=ON -DCMAKE_INSTALL_PREFIX=/usr/localmake -j$(nproc)sudo make installAfter installation, verify the availability of the protobuf compiler and plugin:
protoc --versionwhich grpc_cpp_pluginDefining a Service via a .proto File
Create a file example.proto:
syntax = "proto3";
package example;
service Greeter { rpc SayHello (HelloRequest) returns (HelloReply);}
message HelloRequest { string name = 1;}
message HelloReply { string message = 1;}Generate the C++ code:
protoc --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` example.protoAs a result, the following files will appear: example.pb.h, example.pb.cc, example.grpc.pb.h, example.grpc.pb.cc.
Implementing the Server in C++
Create a file server.cc:
#include <iostream>#include <memory>#include <string>
#include <grpcpp/grpcpp.h>#include "example.grpc.pb.h"
using grpc::Server;using grpc::ServerBuilder;using grpc::ServerContext;using grpc::Status;using example::Greeter;using example::HelloRequest;using example::HelloReply;
class GreeterServiceImpl final : public Greeter::Service { Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override { std::string prefix("Hello, "); reply->set_message(prefix + request->name()); return Status::OK; }};
void RunServer() { std::string server_address("0.0.0.0:50051"); GreeterServiceImpl service;
ServerBuilder builder; builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; server->Wait();}
int main() { RunServer(); return 0;}Implementing the Client in C++
Create a file client.cc:
#include <iostream>#include <memory>#include <string>
#include <grpcpp/grpcpp.h>#include "example.grpc.pb.h"
using grpc::Channel;using grpc::ClientContext;using grpc::Status;using example::Gree Also in library
CsvHelper for C# (.NET): Complete Guide with Examples
Database Migrations in Java: Overview of Popular Tools
Parsing HTML in C++: Overview of Gumbo, MyHTML, and htmlcxx Libraries
Flurl for C# (.NET): Complete Guide with Examples