当你使用 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);
}
}
按类别查看类似文章:
C/C++ ESP8266/ESP32 PlatformIO
如果这篇文章对您有帮助,请考虑请我喝杯咖啡或通过 PayPal 捐款,以支持 TechOverflow 上新文章的研究与发布