如何修复 ESP-IDF 'undefined reference to app_main'

问题:

你正在尝试使用 ESP-IDF 框架编译 C/C++ ESP8266/ESP32 固件。你的源代码看起来像这样:

main_example.cpp
int main() {
    // ...
}

但你只看到类似这样的错误消息:

espidf_linker_error_output.txt
.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\nodemcu-32s\esp-idf\esp32\libesp32.a(cpu_start.c.o):(.literal.main_task+0x18): undefined reference to `app_main'
.platformio/packages/toolchain-xtensa32/bin/../lib/gcc/xtensa-esp32-elf/8.2.0/../../../../xtensa-esp32-elf/bin/ld.exe: .pio\build\nodemcu-32s\esp-idf\esp32\libesp32.a(cpu_start.c.o): in function `main_task':
.platformio\packages\framework-espidf\components\esp32/cpu_start.c:540: undefined reference to `app_main'
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nodemcu-32s\firmware.elf] Error 1

解决方案

对于 ESP-IDF 框架,main() 函数需要命名为 app_main()

app_main_example.cpp
int app_main() {
    // ...
}

完整示例请参见PlatformIO ESP-IDF ESP32 blink 示例


Check out similar posts by category: C/C++, Embedded, PlatformIO