Page 1 of 2

undefined reference to `app_main'

Posted: Wed Nov 24, 2021 2:04 pm
by zhn1021256354
PS E:\esp32\sdk\app\source> idf.py build
Executing action: all (aliases: build)
Running ninja in directory e:\esp32\sdk\app\source\build
Executing "ninja all"...
[1/5] Performing build step for 'bootloader'
ninja: no work to do.
[2/3] Linking CXX executable source.elf
FAILED: source.elf
cmd.exe /C "cd . && E:\esp32\sdk\.espressif\tools\xtensa-esp32-elf\esp-2021r1-8.4.0\xtensa-esp32-elf\bin\xtensa-esp32-elf-g++.exe -mlongcalls -Wno-frame-address @CMakeFiles\source.elf.rsp -o source.elf && cd ."
e:/esp32/sdk/.espressif/tools/xtensa-esp32-elf/esp-2021r1-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/freertos/libfreertos.a(port_common.c.obj):(.literal.main_task+0x14): undefined reference to `app_main'
e:/esp32/sdk/.espressif/tools/xtensa-esp32-elf/esp-2021r1-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/freertos/libfreertos.a(port_common.c.obj): in function `main_task':
E:/esp32/sdk/esp-idf/components/freertos/port/port_common.c:129: undefined reference to `app_main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1



但是我的app_main()函数是可以找到的,怎么破?????

Re: undefined reference to `app_main'

Posted: Sat Nov 27, 2021 12:57 pm
by abansal22
Seems like you want are trying to compile as cpp file.
for that you put the declaration as
extern void app_main()

Re: undefined reference to `app_main'

Posted: Wed Apr 06, 2022 4:07 pm
by smkaranam
I have the exact same issue and I am not using CPP file.

c:/espressif/tools/xtensa-esp32-elf/esp-2021r2-patch2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/freertos/libfreertos.a(port_common.c.obj): in function `main_task':
C:/Espressif/frameworks/esp-idf-v4.4/components/freertos/port/port_common.c:125: undefined reference to `app_main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

Any pointers? Thank you.

Re: undefined reference to `app_main'

Posted: Thu Apr 07, 2022 2:08 am
by ESP_Sprite
Do you have a source file which defines an app_main() function? And if so, is your CMakeLists.txt configured to compile it?

Re: undefined reference to `app_main'

Posted: Thu Apr 07, 2022 3:54 am
by abansal22
smkaranam wrote:
Wed Apr 06, 2022 4:07 pm
I have the exact same issue and I am not using CPP file.

c:/espressif/tools/xtensa-esp32-elf/esp-2021r2-patch2-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld.exe: esp-idf/freertos/libfreertos.a(port_common.c.obj): in function `main_task':
C:/Espressif/frameworks/esp-idf-v4.4/components/freertos/port/port_common.c:125: undefined reference to `app_main'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
ninja failed with exit code 1

Any pointers? Thank you.

did you try to compile the hello world example present in the example folder in esp-idf directory?

Re: undefined reference to `app_main'

Posted: Sun Jun 05, 2022 1:36 pm
by kyo4229
Check the CMakeLists.txt file of the source code directory ,add it into compile.

# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.
#FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)
#idf_component_register(SRCS ${app_sources})
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "")

Re: undefined reference to `app_main'

Posted: Sun Jun 05, 2022 1:58 pm
by kyo4229
Check the \esp-idf\tools\cmake\project.cmake file, it define the component directory of CMakeLists.txt.
if(EXISTS "${CMAKE_CURRENT_LIST_DIR}/main")
__project_component_dir("${CMAKE_CURRENT_LIST_DIR}/main")
endif()

So if the app_main function directory is not this main directory, need to modify the project.cmake file.

Re: undefined reference to `app_main'

Posted: Mon Jan 08, 2024 5:56 pm
by dana2048
This happens when main.c (or similar) is not listed as a component in project_description.json

To fix this run:
idf.py reconfigure

https://docs.espressif.com/projects/esp ... ystem.html

Re: undefined reference to `app_main'

Posted: Fri Feb 16, 2024 8:05 am
by Zyx106
I noticed that on newer esp-idf versions, the reference to 'app_main' only works when the main folder of the project is named "main".
Had it already twice, I think both times with an esp32S3. Lost a lot of time but now I have already changed it in all my projects, even some examples have folder names like "src" where the main.cpp is, the fix for me was to change it to "main".

Re: undefined reference to `app_main'

Posted: Wed Apr 17, 2024 7:06 am
by Vinay Janewad
Hi. I am also facing the same issue. I am trying to integrate Matter protocol with an existing IDF project using ESP Matter SDK and ESP32-S3 chip. I am using ESP IDF v5.2.1. @Zyx106 can you please share your CMakeLists file? Thank you.