GRPC C++ with CMake and Docker
Introduction
This article provides a code template to greatly simplify and automate the use of GRPC in a C++ project. The template is built using CMake. A Docker image consisting of all GRPC files and dependencies is also provided for a C++ project.
The following tools will be used in this project:
- GRPC
- C++
- CMake
- Docker
Learning Outcome
At the end of this project, we should be able to:
- Easily write and run GRPC code in C++ without worrying about the GRPC setup for C++.
- Build GRPC in C++ code with CMake and Docker.
Code
Find the source code in the repository.
Key components of code template
- Key files in this template are
grpc.cmake
,CMakeLists.txt
, andDockerfile
. - The
grpc.cmake
file automatically- installs GRPC via CMake’s
FetchContent
module at configure time, - generates the C++ sources from the
.proto
files, and - performs all necessary file linkage.
- installs GRPC via CMake’s
Instructions
- Set appropriately or disable the proxy settings in the
CMakeLists.txt
andDockerfile
files, before running the code. - Before using the
Dockerfile
, disable the sections on “Fix Git” and “Fix Git proxy”. These sections were only meant to fix issues present in my personal internet access network.
Local: Run code template
- Download the repository
$ git clone https://github.com/Adaickalavan/grpc-cpp-cmake.git
- Build source codes using CMake in Linux
$ cd /path/to/repository/grpc-cpp-cmake/ $ mkdir build $ cd build $ cmake .. $ cmake --build .
- Run the C++ server
$ cd /path/to/repository/grpc-cpp-cmake/ $ cd build $ ./app/greeter_server
- Run the C++ client in a different terminal
$ cd /path/to/repository/grpc-cpp-cmake/ $ cd build $ ./app/greeter_client
Docker: Run code template
- Build image:
$ cd /path/to/repository/grpc-cpp-cmake $ docker build -t grpccpp --network=host .
- Run the container, which runs the C++ GRPC server.
$ cd /path/to/repository/grpc-cpp-cmake $ docker run --rm --network=host grpccpp
- Run the C++ GRPC client in a different local host terminal
$ cd /path/to/repository/grpc-cpp-cmake/ $ cd build $ ./app/greeter_client
Local: Build and install GRPC
For most of us, we should build and run all our GRPC C++ code locally by following the instructions given above. Ideally this step should not be performed, as it has been simplified by the above steps. The steps below are only provided for those who are interested in manually installing GRPC system-wide in their computer. See here for further reference.
- Set
~/.bash_aliases
or~/.bashrc
file:export MY_INSTALL_DIR=$HOME/.local export PATH=$PATH:$MY_INSTALL_DIR/bin
- Install pre-requisites and CMake
$ sudo apt-get install -y build-essential autoconf libtool pkg-config $ sudo apt-get install -y cmake
- Clone gRPC repository
$ git clone -b v1.28.1 https://github.com/grpc/grpc $ cd /path/to/repository/grpc $ git submodule update --init
- Builds and install gRPC
$ cd /path/to/repository/grpc $ mkdir -p cmake/build $ cd cmake/build $ cmake -DgRPC_INSTALL=ON \ -DgRPC_BUILD_TESTS=OFF \ -DCMAKE_INSTALL_PREFIX=$MY_INSTALL_DIR \ ../.. $ make $ make install
Leave a comment