Last updated:2026-10-12
A Small C++ gRPC Project¶
A Small gRPC Demo from C++那些事¶
Popular RPC frameworks on GitHub include gRPC and brpc, along with Tencent's powerful internal trpc and others; these RPC frameworks support different languages and platforms. Today let's talk about how to use gRPC, starting from a simple demo. The whole project is built with CMake — a very standard way to manage an RPC project. All the code has been updated to the Knowledge Planet community; see the QR code at the end for how to get it.
1. Step-by-Step Environment¶
If your computer is a Mac, sorry — for now you cannot use the latest version of gRPC. The latest gRPC uses abseil, and compilation has problems that the official team has not resolved yet. If you know how to solve this, please leave a comment; I have not found a solution so far.
So, it is recommended to install a lower version of gRPC on your Mac, or install directly on Linux, where the latest gRPC works — installed in one go without any problems.
Installation:
Then initialize the submodules:
Build and install with CMake:
cd cmake
mkdir build
cd build
cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=/usr/local/grpc ../..
make -j8
make -j8 install // root 权限
You can change the installation directory above; note that root privileges are required at install time.
After installation, add its bin directory to your PATH, for example:
2. String Reversal Demo¶
With the environment ready, we can get our hands dirty.
2.1 The Protocol¶
The first thing is to define the RPC protocol:
syntax = "proto3";
package stringreverse;
service StringReverse {
rpc sendRequest (StringRequest) returns (StringReply) {}
}
message StringRequest {
string original = 1;
}
message StringReply {
string reversed = 1;
}
A very simple protocol: you request with one string and get one string back.
2.2 The Client¶
Build the RPC request string, then just call the sendRequest interface to send it out.
std::string str = "hello world grpc!";
std::cout << "Original string: " << str << std::endl;
StringRequest request;
StringReply reply;
ClientContext context;
request.set_original(str);
Status status = stub->sendRequest(&context, request, &reply);
if (status.ok()) {
std::cout << "Reversed string: " << reply.reversed() << std::endl;
}
// do something
2.3 The Server¶
Implement the string reversal logic by inheriting from Service.
class ReverseServiceImplementation final : public StringReverse::Service {
Status sendRequest(ServerContext* context, const StringRequest* request,
StringReply* reply) override {
// 反转逻辑
}
}
Finally, just register the service:
builder.RegisterService(&service);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on port: " << server_address << std::endl;
3. Running¶
Create a build directory:
mkdir build
cmake ..
# 输出
-- The C compiler identification is GNU 10.2.1
-- The CXX compiler identification is GNU 10.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /opt/rh/devtoolset-10/root/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /opt/rh/devtoolset-10/root/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found Protobuf: /usr/local/grpc/bin/protoc-23.4.0 (found version "23.4.0")
-- Using protobuf
-- Using gRPC 1.57.0
-- Configuring done
-- Generating done
-- Build files have been written to: /code/stringreverse/build
Build with make:
[ 10%] Generating stringreverse.pb.cc, stringreverse.pb.h, stringreverse.grpc.pb.cc, stringreverse.grpc.pb.h
Scanning dependencies of target server
[ 20%] Building CXX object CMakeFiles/server.dir/server.cc.o
[ 30%] Building CXX object CMakeFiles/server.dir/stringreverse.pb.cc.o
[ 40%] Building CXX object CMakeFiles/server.dir/stringreverse.grpc.pb.cc.o
[ 50%] Linking CXX executable server
[ 50%] Built target server
Scanning dependencies of target client
[ 60%] Building CXX object CMakeFiles/client.dir/client.cc.o
[ 70%] Building CXX object CMakeFiles/client.dir/stringreverse.pb.cc.o
[ 80%] Building CXX object CMakeFiles/client.dir/stringreverse.grpc.pb.cc.o
[ 90%] Linking CXX executable client
[100%] Built target client
Run the server:
Run the client:
[light@i-i339drlk build]$ ./client
Original string: hello world grpc!
Reversed string: !cprg dlrow olleh
Related Topics¶
- Hand-Writing C++ FnOnce | C++那些事
- C++ Inter-Process Communication in Practice | C++那些事
- C++ Short URL Service Demo | C++那些事
- C++17 and SQLite Folder Size Statistics | C++那些事
- Google Benchmark Performance Testing | C++那些事
- C++20 Coroutine HTTP Server | C++那些事
- Protobuf ZeroCopy Serialization | C++那些事
- Accelerating Python with pybind11 | C++那些事
- C++ Stack Calculator Design | C++那些事