So like the previous topic - Play with Makefile for 1st time, I hope you and me can produce the makefile for helping compile C++ project, especially gRPC - pretty hard when work with C++ than others language. So take a look and maybe we will learn about CMake. Iβ dunno but I will try πππ
Hard to try gRPC with Makefile
Info
Base on the official project from gRPC, Make is deprecated optional although default choice when build for UNIX based system used this one. Recommendation, should use bazel or cmake instead
Quote
Upset, but you need to know, there is not easily to managing the gRPC and add-ons pluggin when you write the makefile for yourself and compile project, Itβs really tough, hard and waste much time to understand how you wrong is it, like me πππ
So donβt waste your time, gprc is making some helpful thing compiler with automation generate the completion makefile via CMake. Go quick and check that in official homepage of CMake
Quick setup gRPC
How can setup grpc ?
Abstract
Why gRPC ?
gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
Feature gRPC
Simple service definition
Start quickly and scale
Works across languages and platforms
Bi-directional streaming and integrated auth
So, with summary about gRPC - the brief from official homepage - Maybe it will help you gain your knowledge about this technologies which help you in some case like
Increase your transfer performance
Improving the security for bi-direction data
β¦
Quote
But theory is just theory, knowing what it is and how to apply it is important.Therefore, come from the C++/C, the low level of code, that will help you understand how it work and implementation.But first of all, how we can setup the environment for gRPC ? easy or hard ? I am the first try, I choose the hardest thing - Installed the gRPC from multiple project by myself.
This bash script is created for first time install gRPC, nightmare !!!. It generate fail grpc, not including package, β¦
So maybe choose believe yourself, you should learn from gRPC and It will suggest you about optional to install it, that helpful link CPP - Quick start is the manual which help you. Secondly change, I donβt have made another mistake when install gRPC, just put bash script and you will have what you want
This script is better way and collected the multiple task from gRPC official documentation, It will help you install gRPC and adds-on plugin attach about 10 or more. So just run the script and your workspace will have gRPC (Test on Ubuntu 22-04, Debian and will not work with another UNIX, Linux Distros)
Some tips to setup gRPC for your C++/C workspace
This recommendation just for Ubuntu 22.04 with shell like zsh or bash)
Export PATH for to used bin module like protoc
Optional 1: Setup for only currently session
Optional 2: Setup for persistent in Linux (Ubuntu 22.04 zsh or bashrc)
That all, you have the new PATH, you can check via command to knowing right or not
Setup gRPC including and package requirements PATH
Warning
Easily for development to ignoring some warning and error messages when not finding the include package for project dependencies
Like the first tip, you need export some variables into your default shell. This will help your editor know location of package, find right PATH is easily for coding and suggestion. This list of will help you know more and more
CMake is the de-facto standard for building C++ code, with over 2 million downloads a month. Itβs a powerful, comprehensive solution for managing the software build process. Get everything you need to successfully leverage CMake by visiting our resources section.
With gRPC, CMake is very helpful and powerful. Actually, CMake is just generate tool and help you create Makefile, but it will optimize the effort when it is not easily to creating by yourself (thousand lines)
Implementation CMake for gRPC project
This script is created by me which base on some collection script from gRPC, github, β¦ for suitable applied on project structure below. Therefore, I think those helpful documentations do for something πππ
First of all, you can make project directory like me
Create CMakeLists.txt in root directory, the contents is about and you need common.cmake to handle others work βΆοΈ βΆοΈ common.cmake (NOTICE: add_subdirectory(../../.. ${CMAKE_CURRENT_BINARY_DIR}/grpc EXCLUDE_FROM_ALL) changing this PATH for suitable with your project)
In head of file, you will define CMake version, what language C or C++ used, include common.make
After that, specify PATH for protos directory and your location expected when proto is generated. It will help you create gRPC file and header to support your proto
Last one, you need to add some PATH of your project, dynamic link and requirement lib to build gRPC. With responsibility, CMAKE will collect and import into the module generated when run build.
Compile and Enjoy the results
To compile this project you need something to do with that, so with reference, I just copied and renewed C++/C for doing this things. Take a look
Preparation completely, next you need to compile this project into binary and running that. Go some command below and enjoy results
Create CMake folder to build, simple create build folder inside cmake folder, add this directory into stack to easily control when switch dev and build task
Compile your project with CMake and Makefile generated
Results is will the twice binary file include 1 server and 1 client. Just run execution file in Linux with ./<name_of_file>
Some Addon after you understand gRPC compiling
You can switch from stack and normal shell with command pushd <directory> and popd
Tips
With these command, you will not mess up your terminal and fear when compile gRPC, Itβs actually helpful when you need to fast compile and donβt want to wrong PATH
You can recompile your code to build a new proto file, new code with run make again, anything will up to date
Clean everything on CMake, Just run make clean
Create dev environment for ignore not found packages
Tips
Please, Run in root directory with makefile below which will help you creating *.cc and *.h files base on *.proto
When want to create proto support, just run make -f <makefile> <nameproto>.grpc.pb.cc <nameproto>.pb.cc
When want to clean to create a new one because when make exist file, you canβt not running to update (NOTICE: Add .PHONY but maybe in later), just run make -f <makefile> clean
Conclusion
Abstract
I hope this article is choice for your first gRPC project compiled with C++. This is really basic, so expand it base on your idealy, maybe It can help you. On some other, I will contribute about CMake to understanding this technologies and find out best way to create CMake. Enjoy and have fun ππππ