How to use C++ main.cpp with PlatformIO on ESP32 / esp-idf
When you are using PlatformIO to compile your firmware, it is easily possible to use a C++ main.cpp instead of the pure C main.c by just renamingmain.c to main.cpp
However, you also need to properly declary app_main(). After the #include section of your main.cpp (or at the top of the file if you don’t have an #include section yet), add this code:
app_main_declare.cpp
extern "C" {
void app_main(void);
}This will tell the compiler that app_main() is a C function, not a C++ function.
Full main.cpp example
espidf_main_cpp_example.cpp
#include <cstdio>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
extern "C" {
void app_main(void);
}
void app_main() {
while(true) {
printf("Hello PlatformIO!\n");
// Wait for one second
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}Check out similar posts by category:
C/C++, ESP8266/ESP32, PlatformIO
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow