Work smart and have code generated for you by your makefile! This example will demonstrate how to set up a project with Embedded Proto using Makefiles. All the *.proto files will automatically be detected; each source file code will be generated and built.

The following topics will be addressed:
1. Setup the project
2. Initiating Embedded Proto
3. Including the generated headers
4. Automatically convert *.proto files
5. Building the project

In this example, we assume you have already installed all the requirements for Embedded Proto. The requirements and installation instructions for your system are described in the Installation section on the documentation page.

1. Setup the project

In this example, we will use the following folder structure:

tree -L 1
.
├── src
├── proto
├── build
├── embeddedproto
└── Makefile

The src folder contains the source files for this example. The example code on GitHub is based on a NUCLEO-F466RE board by ST Microelectronics. The STM HAL is placed in the source folder to support the board.

All *.proto files for this example are located in the proto folder. There are two in this case request.proto and reply.proto.

When building, all build results will be placed in the build folder.

After cloning the example repository, you will not yet have the embeddedproto folder. Here we are using Embedded Proto as a git submodule which still needs to be initiated. The following chapter describes how you can do the same for your project.

2. Initiating Embedded Proto

If you are working from the example repository you can check it out and pull the submodule in one command:

git clone --recurse-submodules git@github.com:Embedded-AMS/EmbeddedProto_Example_Makefile.git

If that is the case you can skip the rest and start reading the next section.

Now, if you are creating your project you better make a folder for it. After creating your project folder, change your current directory to the new folder. In that folder, we will add Embedded Proto as a submodule. After the clone is complete, enter the folder and checkout the latest version. To keep track of the version of Embedded Proto you are using in your project, add the .gitmodules file and commit it.

git submodule add git@github.com:Embedded-AMS/EmbeddedProto.git embeddedproto
cd embeddedproto
git checkout latest
cd -
git add .gitmodules
git commit –m "Added the Embedded Proto library as a submodule on the latest version."

Either way, the submodule still needs its virtual environment with the code generator. Enter the folder and run the install script once, as described on the installation page. The embeddedproto command the makefile uses lives in that environment.

cd embeddedproto
python install.py
cd -

3. Including the generated headers

When using wildcards in a makefile the wildcard is executed before built targets. Therefore we can not generate the source files in the first target, add the new files using a wildcard and build the new code in the second target. A solution is to add the filenames we need by constructing them in the makefile before the source code exits.

First, let us define some variables we need often.

# Define where the *.proto files are located.
PROTO_DIR = proto

# Define the output directory in which to place the build results.
PROTO_GEN_DIR = build/generated

# The location of the Embedded Proto submodule and the code generator in its virtual environment.
EMBEDDED_PROTO_DIR = embeddedproto
EMBEDDEDPROTO = $(EMBEDDED_PROTO_DIR)/venv/bin/embeddedproto

Next, we find all the *.proto files in the project. For this, we do use a wildcard expression.

# Find all the proto files.
# Extend this for subfolders.
PROTO_FILES = $(wildcard $(PROTO_DIR)/*.proto)

Based on the *.proto file names we can change the extension of the files to *.h and store those in a new variable.

# Convert the names of the proto files to the name of the 
# generated header files.
PROTO_HDR := $(PROTO_FILES:%.proto=$(PROTO_GEN_DIR)/%.h)

Messages generated by Embedded Proto are pure header files without *.cpp files, and as of version 4.0.0 the library itself is header only as well. There is nothing of Embedded Proto to compile. All the makefile needs is two include paths: the src folder of the submodule and the folder with the generated headers.

# Define directories to include.
INC_DIR = -Isrc \
          -I$(PROTO_GEN_DIR) \
          -I$(EMBEDDED_PROTO_DIR)/src

The library uses if constexpr, so compile your C++ sources with -std=c++17 or newer.

4. Automatically convert *.proto files

So all the *.proto files are located in the proto folder. How do we ensure that the makefile finds them and generates code? A build target is defined, which will generate the header files based on the *.proto files. The build target called generate does not call the generator itself. By adding the list with header filenames as requirements, an implicit build target for each header will be called.

###############
# Major targets
###############
generate: $(PROTO_HDR)
	$(info Done generating source files based on *.proto files.)

##################
# Implicit targets
##################

# This rule is used to generate the message source files based 
# on the *.proto files.
$(PROTO_GEN_DIR)/%.h: %.proto
	$(shell mkdir -p $(dir $@))
	$(EMBEDDEDPROTO) -I$(PROTO_DIR) --eams_out=$(PROTO_GEN_DIR) $<

The implicit target makes sure the output folder exists, and it runs the embeddedproto command. The command runs the protobuf compiler which came with the package and adds the plugin and its include folders for you, so there is no protoc to install and no plugin path to get right. See generating source code.

The parameters pass the folder with the *.proto files as include folder, the output folder for the generated headers, and the *.proto file being processed.

5. Building the project

The final step is to build the project. You can do this simply by adding the generate target as a requirement for your debug, release, and all targets. This way, you can call make generate to just generate the message file or just make to build the whole project without having to call generate separately.

Changes in your proto files will be picked up by the makefile and considered in your next build.

More examples

All examples