ESP8266 Espressif SDK programming in C++


Are you a C++ crazy fan? I’m! don’t be shy. I can’t imagine life without programming in C++, so now that I’m getting into IoT I wanted to explore the feasibility of using C++, after all programming ESP8266 in the Arduino IDE you can, why can’t we on the official SDK?

Out of the box the projects build on top of Espressif SDK are compiled in C. And that’s a good thing. A better one is to program our projects in C++. I didn’t find any useful information, so I tried it myself, and I succeded!

So far I just tried to use my own classes, I haven’t tested the STL library, but they would work (I don’t want to forget the steps I followed, so I’m in a hurry!).


I tried theses steps in Linux Mint, but there’s nothing weird that cannot be done in either Windows or Mac. I’m assuming you have an Espressif SDK environment up and running.

My setup is so simple:

Setup using a ESP-01 board on top of DIY programmer.

STEP 1: Do everything is needed to compile your projects in C under the Espressif SDK. This is a great introduction.

STEP 2: Say you’re following the «Hello world» example. Look for the file hello_world_main.c under the hello_world/main folder, and delete it.

STEP 3: In that same folder create a new file called hello_world_main.cpp and populate it with the next content:

/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.

   Modified by fjrg76, 26/04/21, Mexico City

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"

class MyClass
   int a;

   void set( int val )
      a = val;

   int get()
      return a;

int f()
   MyClass c;

   c.set( 100 );

   return c.get();

// app_main() is the entry point for our programs and it must be a C function:

#ifdef __cplusplus
extern "C"{

void app_main()
   printf("Hello world!\n");

   /* Print chip information */
   esp_chip_info_t chip_info;
   printf("This is ESP8266 chip with %d CPU cores, WiFi, ",

   printf("silicon revision %d, ", chip_info.revision);

   printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
         (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

   for (int i = 10; i >= 0; i--) {
      printf("Restarting in %d seconds...\n", i);
      vTaskDelay(1000 / portTICK_PERIOD_MS);

   // for testing the C++ compiler:
   int v = f();
   printf( "Value is: %d\n", v );
   vTaskDelay( 500 / portTICK_PERIOD_MS );

   printf("Restarting now.\n");

#ifdef __cplusplus

It’s almost the same code as the original example, but I added some things:

  1. The most important fact to be aware is that the function app_main() is surrounded with the macro __cplusplus in order to be compiled as C function. Otherwise it won’t work. The entry point for the user code is the function app_main() and it must be a C function.
  2. I added a silly class and a silly function f(). Actually this f() function might be the entry point for our programs (if you aren’t using FreeRTOS tasks).
  3. Inside the function app_main() I called the function f() so that it is compiled, and we use the instance c from the class MyClass.

You’ll end up with these 3 files:

STEP 4: Open the file CMakeLists.txt and modify it with this next content:

idf_component_register(SRCS "hello_world_main.cpp"
                    INCLUDE_DIRS "")

The only change with regards to the original one is that I’ve changed the extension from .c to .cpp.

STEP 5: Build the project and enjoy programming the ESP8266 in C++ along the real time operating system FreeRTOS:

$ make clean
$ make 

The make clean instruction is to clean up the environment. It contains debris from those times when the project was compiled with the C compiler. Once done you don’t need to repeat it.

The make instruction actually compiles the project using the C++ compiler. If you get this next output, you’re done!

STEP 5: Upload your program into the chip:

$ make flash

You don’t need to emit two instructions: make and then make flash; with the latter is enough. I used a single make so you can see the output.

Output after flashing our program into the chip.

You might want to see what the program is doing. Press and release the reset button on your board (I need to do it by hand on mine) and type:

$ make monitor

(To exit from the monitor type Ctrl + ]). This is the output:

Program output. Look at the line before the last line. That’s the result of calling our class MyClass.

What’s next

We’ve seen how to program using C++; the next logical step is to try the STL.

If you speak spanish, or at least you understand it, and you aren’t aquainted with FreeRTOS, you might want to take a look to my free course on «Arduino en tiempo real» (Arduino in real time).

If you like this post please drop me a line to say «Hello!». Or better off, suscribe!

Anuncio publicitario

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de

Estás comentando usando tu cuenta de Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s