gRPC C++ Tutorial¶
In this document, it is aimed to explain the gRPC server client structure from scratch with C++ through a simple example on a Linux based PC.
For the fastest and most reliable installation, gRPC Quick Start reference will be taken.
Overview¶
Some hardware features (Memory, CPU, ...) of the PC on which the gRPC is run will be printed on the terminal screen.
Setup¶
Choose a directory to hold locally installed packages. This page assumes that the MY_INSTALL_DIR environment variable holds this directory path. This variable name will be used throughout the document. The variable content will be kept unless the terminal, which is opened for the installation, is closed.
export MY_INSTALL_DIR=$HOME/.local
mkdir -p $MY_INSTALL_DIR
export PATH="$PATH:$MY_INSTALL_DIR/bin"
Prerequisities¶
cmake¶
sudo apt install -y cmake
cmake --version
gRPC and protocol buffers¶
Install the basic tools required to build gRPC¶
sudo apt install -y build-essential autoconf libtool pkg-config
Clone the gRPC repo and its submodules¶
git clone --recurse-submodules -b v1.33.2 https://github.com/grpc/grpc
cd grpc
Build and locally install gRPC and all required tools¶
$ mkdir -p cmake/build
$ pushd cmake/build
$ cmake -DgRPC_INSTALL=ON \
-DgRPC_BUILD_TESTS=OFF \
-DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \
../..
$ make -j
$ make install
$ popd
Getting the Code¶
Getting sample project¶
Download the tutorials from the following link:
Then extract the archive:
tar xzvf tutorials.tar.gz
Build the code with cmake¶
Change to the cpp-samples/systemservice
directory.
mkdir -p cmake/build
pushd cmake/build
cmake -DCMAKE_PREFIX_PATH=$MY_INSTALL_DIR ../..
make -j
The final layout of the project directory should look as following:
├── client.cc
├── cmake
│ └── build
│ ├── client
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ │ ├── 3.13.4
│ │ │ ├── CMakeCCompiler.cmake
│ │ │ ├── CMakeCXXCompiler.cmake
│ │ │ ├── CMakeDetermineCompilerABI_C.bin
│ │ │ ├── CMakeDetermineCompilerABI_CXX.bin
│ │ │ ├── CMakeSystem.cmake
│ │ │ ├── CompilerIdC
│ │ │ │ ├── a.out
│ │ │ │ ├── CMakeCCompilerId.c
│ │ │ │ └── tmp
│ │ │ └── CompilerIdCXX
│ │ │ ├── a.out
│ │ │ ├── CMakeCXXCompilerId.cpp
│ │ │ └── tmp
│ │ ├── client.dir
│ │ │ ├── build.make
│ │ │ ├── client.cc.o
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── CXX.includecache
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── progress.make
│ │ │ ├── System.grpc.pb.cc.o
│ │ │ └── System.pb.cc.o
│ │ ├── cmake.check_cache
│ │ ├── CMakeDirectoryInformation.cmake
│ │ ├── CMakeError.log
│ │ ├── CMakeOutput.log
│ │ ├── CMakeRuleHashes.txt
│ │ ├── CMakeTmp
│ │ ├── feature_tests.bin
│ │ ├── feature_tests.c
│ │ ├── feature_tests.cxx
│ │ ├── Makefile2
│ │ ├── Makefile.cmake
│ │ ├── progress.marks
│ │ ├── server.dir
│ │ │ ├── build.make
│ │ │ ├── cmake_clean.cmake
│ │ │ ├── CXX.includecache
│ │ │ ├── DependInfo.cmake
│ │ │ ├── depend.internal
│ │ │ ├── depend.make
│ │ │ ├── flags.make
│ │ │ ├── link.txt
│ │ │ ├── progress.make
│ │ │ ├── server.cc.o
│ │ │ ├── System.grpc.pb.cc.o
│ │ │ └── System.pb.cc.o
│ │ └── TargetDirectories.txt
│ ├── cmake_install.cmake
│ ├── Makefile
│ ├── server
│ ├── System.grpc.pb.cc
│ ├── System.grpc.pb.h
│ ├── System.pb.cc
│ └── System.pb.h
├── cmake_externalproject
│ └── CMakeLists.txt
├── CMakeLists.txt
├── Makefile
├── server.cc
└── System.proto
Run the example¶
Ensure you are in the $iedk-samples/cpp-samples/systemservice/cmake/build
directory. Then open a terminal and run the gRPC server:
./server
Open another terminal and run the gRPC client:
./client
Sample output¶
Product: VMware Virtual Platform
Distro: debian 10 buster
Kernel: 4.19.0-11-amd64 GNU/Linux
Board: Intel Corporation VMware Virtual Platform Intel Corporation 82371AB/EB/MB PIIX4 ISA (rev 08)
Cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz, load:14.3
Memory: 15G total,2.4G used, 11G free, 12G avail,
Disk: VMware Virtual I /dev/sda 200G
Conclusion¶
Developing a gRPC application is based on the idea of defining a service
, specifying the methods that can be invoked remotely with their parameters and return types. Take care when installing the cmake. If any compilation errors are received, the necessary steps must be repeated from the start.