An IoT Mastery Project guide series
Split main.cpp into multiple files
In this article, I’ll show you how to split the code that is currently in main.cpp into multiple files. To do this, first we will look at what parts of main.cpp can go into their own individual files.
In this article, I’ll show you how to split the code that is currently in main.cpp into multiple files.
Deciding on what to remove from main.cpp
To do this, first, look at what parts of main.cpp can go into their own individual files.
The refresh_readings() function in main.cpp is a good candidate since it is a function that deals specifically with the sensor. That is, it has a well-defined, specific purpose.
I can create a new file called something like "sensor_readings" and then put this content in that new file.
Also, at the top of main.cpp are a few things that can be moved into new file, like inclusions and definitions that eventually will be shared with other elements across the program.
I can see the case whereas the project progresses, multiple files will need access to those settings. So, it also makes sense to remove some of the code in the header of main.cpp and put it in a separate file, perhaps called something like "settings".
Then, we’ll link those files with main.cpp, so that main.cpp can operate properly, as it is now.
Create a new file
Let's begin and create a new file. In the Explorer pane, click on the new file button.
In the text box that appears, enter the name for the new file: "sensor_readings.cpp".
In C++, we typically use the "cpp" extension for files that contain the implementation of our program and the "h" extension ("h" stands for “header”), for files that contain the declaration for things such as variables and functions.
Now, we have a new empty file, called "sensor_readings.cpp" (1, see below). It is located in the "src" directory (2) and it is currently empty (3). Double-click on “sensor_readings.cpp” to open the file.
Let’s add some code. Go back to main.cpp.
Copy the code in the refresh_readings() function:
Paste it in sensor_readings.cpp (and remove it from main.cpp).
The new file now looks like this:
Code has identified a few issues with the code, and marked them with a red curly line. We’ll fix those issues shortly.
And save the file.
Commit the change to the repository
We have only made a small change to our code, and without some fixes the program will not compile. However, I think this is a good opportunity to do some work with the repository.
Go into the terminal down here and check on the status of the GitHub repository.
The result will look something like this:
Git has detected a change in the main.cpp file, and also a new file "sensor_readings.cpp". There is also a third file inside the .vscode directory which was created by the editor.
The same information appears in the source control tab:
The letter “M” next to main.cpp indicates that this file is tracked and contains changes that are not committed. The red letter “U” next to “sensor_readings.cpp” indicates that this file is un-tracked by Git.
Just like we did in the previous article, we can add the new file to the index so that Git can track it. Then, we can commit it to the repository.
In the terminal, type
git add .
The “.” is a shortcut that means “all”. This Git command will add all untracked files to the index.
Check the status again:
So we've got a new file it's now being tracked. But we haven't committed our actual changes yet.
Let’s do that now. Type:
git commit -am “Moved sensor code to new file.”
The terminal should look like this:
How to link code across files
Go into the "sensor_readings.cpp" tab, and you'll see multiple curly lines indicating problems.
The IDE has detected references to variables and functions that it does not know anything about.
For example, the Serial class is part of the Arduino framework. "sensor_readings.cpp" contains references to Serial without first linking to it. When the IDE looks inside this specific file, it does so in isolation to anything outside this file, unless it is linked to via an “#include” statement.
To fix this specific problem, simply add “#include <Arduino.h>” at the top of "sensor_readings.cpp".
The result is this:
There is no curly line under Serial, which means that this issue is fixed.
There are other issues remaining, but they are of the same category. The constants “LED_BUILTIN” and “SEALEVELPRESSURE_HPA”, and the object “bme” are undefined in this file, and we have not linked it to file where they are defined.
All of these are currently defined in main.cpp.
In the video I have linked at the top of this page, I show how I methodically work my way towards fixing these issues.
Here, I will give a summary of the process of splitting main.cpp into multiple files.
Continuing with "sensor_readings.cpp", update the file to contain this content:
In the header of this file, notice the inclusions of Arduino.h, and also two new header files:
In sensor_readings.h is the header counterpart of sensor_readings.cpp. As is the convention in C++, this file contains function and variable declarations that are implemented in the cpp file. The header file may also include other headers that contain function and variable definitions used by the code in the cpp file.
Here are the contents of sensor_readings.h:
Take a few minutes to read this code. Notice how some of its content used to be in main.cpp? For example, the definition of SEALEVELPRESSURE_HPA and the inclusion of Adafruit_BME280 was in main.cpp, but is now in sensor_readings.h
The second new file is “settings.h”. Here are its contents:
This file contains the definition of the LED_BUILTIN constant. But, we will be adding a lot more code in here in other lectures in ESP32 Unleashed, that is used by other parts of the program. I decided it was a good idea to set up this file now.
And what about main.cpp? It is much smaller now. Here are its current contents:
All the code that does the heavy lifting in this program is in sensor_readings.cpp.
Once you finish working on the changes, try to compile the project. It should compile without any issues:
In the header files, notice how I have used the "ifndef" and "endif" keywords? These are pre-compiler statements that segment the code they include. This structure ensure that the enclosed code is only evaluated once during the compilation. This way, there is no risk of defining variables, constants, and functions more than once (which will cause the compiler to emit an error).
Commit changes to Git
Let's check the status of the Git repository. Type:
You should see this:
Two files, main.cpp, and sensor_readings.cpp contain changes. There are also a few new files in the “src” and in “.vscode” directories.
Let's add the new files so that we can start tracking them:
git add .
Check the status:
Here it is:
Let's commit the changes:
git commit -am "Split main.cpp into two new files".
And check the status once again, to be sure:
The source control pane is empty, which is also an indication that there are no changes pending to commit.
So let's move ahead now in the next lecture where we'll test the new version of the application on the board.
Ready for some serious learning?
A new learning adventure awaits you.
Create an application that will stretch your existing knowledge and skills.
ESP32 Unleashed is a project course
This course is perfect for people familiar with the ESP32, especially graduates of ESP32 For Busy People.
It is a guided project, designed to teach you how to use modern tools to create modern embedded applications based on the ESP32.
Just click on the big red button to learn more.
Jump to another article
1: Why use PlatformIO to program the ESP32?
2: How to install PlatformIO and MS Code (Mac OS)
3: How to install PlatformIO and MS Code (Windows 10)
4: Set up an ESP32 project in PlatformIO
5: Upload and test the new project
6: Create the Git repository
7: Split the program into two files
8: Test the multi-file project
9: How to use Git version control