如何在 ESP32 / esp-idf 上使用 C++ main.cpp 与 PlatformIO
当你使用 PlatformIO 编译固件时,通过只需重命名 main.c 为 main.cpp,就可以轻松使用 C++ main.cpp 代替纯 C main.c
但是,你还需要正确声明 app_main()。在 main.cpp 的 #include 部分之后(如果还没有 #include 部分,则在文件顶部),添加此代码:
app_main_declare.cpp
extern "C" {
void app_main(void);
}这会告诉编译器 app_main() 是 C 函数,不是 C++ 函数。
完整 main.cpp 示例
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