Wie man C++ main.cpp mit PlatformIO auf ESP32 / esp-idf verwendet
Wenn Sie PlatformIO verwenden, um Ihre Firmware zu kompilieren, ist es leicht möglich, eine C++-main.cpp statt der reinen C-main.c zu verwenden, indem Sie main.c einfach in main.cpp umbenennen
Sie müssen jedoch auch app_main() korrekt deklarieren. Fügen Sie nach dem #include-Abschnitt Ihrer main.cpp (oder oben in der Datei, wenn Sie noch keinen #include-Abschnitt haben) diesen Code hinzu:
app_main_declare.cpp
extern "C" {
void app_main(void);
}Dies teilt dem Compiler mit, dass app_main() eine C-Funktion ist, keine C++-Funktion.
Vollständiges main.cpp-Beispiel
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