undefined reference to `app_main'

zhn1021256354
Posts: 11
Joined: Sat Aug 28, 2021 4:02 am

undefined reference to `app_main'

Postby zhn1021256354 » Wed Nov 24, 2021 2:04 pm

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()函数是可以找到的,怎么破?????

abansal22
Posts: 105
Joined: Wed Apr 22, 2020 8:24 am

Re: undefined reference to `app_main'

Postby abansal22 » Sat Nov 27, 2021 12:57 pm

Seems like you want are trying to compile as cpp file.
for that you put the declaration as
extern void app_main()

smkaranam
Posts: 1
Joined: Wed Apr 06, 2022 4:03 pm

Re: undefined reference to `app_main'

Postby smkaranam » 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.

ESP_Sprite
Posts: 8921
Joined: Thu Nov 26, 2015 4:08 am

Re: undefined reference to `app_main'

Postby ESP_Sprite » Thu Apr 07, 2022 2:08 am

Do you have a source file which defines an app_main() function? And if so, is your CMakeLists.txt configured to compile it?

abansal22
Posts: 105
Joined: Wed Apr 22, 2020 8:24 am

Re: undefined reference to `app_main'

Postby abansal22 » Thu Apr 07, 2022 3:54 am

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?

kyo4229
Posts: 2
Joined: Sun Jun 05, 2022 1:34 pm

Re: undefined reference to `app_main'

Postby kyo4229 » Sun Jun 05, 2022 1:36 pm

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 "")

kyo4229
Posts: 2
Joined: Sun Jun 05, 2022 1:34 pm

Re: undefined reference to `app_main'

Postby kyo4229 » Sun Jun 05, 2022 1:58 pm

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.

dana2048
Posts: 1
Joined: Mon Jan 08, 2024 5:52 pm

Re: undefined reference to `app_main'

Postby dana2048 » Mon Jan 08, 2024 5:56 pm

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

Zyx106
Posts: 1
Joined: Fri Feb 16, 2024 7:59 am

Re: undefined reference to `app_main'

Postby Zyx106 » Fri Feb 16, 2024 8:05 am

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".

Who is online

Users browsing this forum: No registered users and 44 guests