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

  1. Key files in this template are grpc.cmake, CMakeLists.txt, and Dockerfile.
  2. 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.

Instructions

  • Set appropriately or disable the proxy settings in the CMakeLists.txt and Dockerfile 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

  1. Download the repository
     $ git clone https://github.com/Adaickalavan/grpc-cpp-cmake.git 
    
  2. Build source codes using CMake in Linux
     $ cd /path/to/repository/grpc-cpp-cmake/
     $ mkdir build
     $ cd build
     $ cmake ..
     $ cmake --build .
    
  3. Run the C++ server
     $ cd /path/to/repository/grpc-cpp-cmake/
     $ cd build
     $ ./app/greeter_server
    
  4. Run the C++ client in a different terminal
     $ cd /path/to/repository/grpc-cpp-cmake/
     $ cd build
     $ ./app/greeter_client
    

Docker: Run code template

  1. Build image:
     $ cd /path/to/repository/grpc-cpp-cmake
     $ docker build -t grpccpp --network=host .
    
  2. Run the container, which runs the C++ GRPC server.
     $ cd /path/to/repository/grpc-cpp-cmake
     $ docker run --rm --network=host grpccpp
    
  3. 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.

  1. Set ~/.bash_aliases or ~/.bashrc file:
     export MY_INSTALL_DIR=$HOME/.local
     export PATH=$PATH:$MY_INSTALL_DIR/bin
    
  2. Install pre-requisites and CMake
     $ sudo apt-get install -y build-essential autoconf libtool pkg-config
     $ sudo apt-get install -y cmake
    
  3. Clone gRPC repository
     $ git clone -b v1.28.1 https://github.com/grpc/grpc
     $ cd /path/to/repository/grpc
     $ git submodule update --init
    
  4. 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
    

Updated:

Leave a comment