How do I merge 2 codes with "app_main" and one .c and other .cpp

Harshad P
Posts: 7
Joined: Wed Apr 14, 2021 6:27 am

How do I merge 2 codes with "app_main" and one .c and other .cpp

Postby Harshad P » Mon May 24, 2021 1:15 pm

Hello all,

I am trying to merge two codes (app_main.cpp and main.c) and my folder structure looks like this

Project
#->Cmakelists.txt
#->Makefile
#->sdkconfig
#->Main
### ->Cmakelists.txt
### ->main.c
### ->main.h (I added this)
### ->app_main.cpp

Both main.c and app_main.cpp have "void app_main()"
So my approach was to change "app_main()" to "c_app_main()" in main.c and also removed while(1) since I have it in app_main.cpp. I also added "main.h" and declared "c_app_main()".
I updated the Cmakelists.txt to compile both app_main.cpp and main.c

Now I am getting this error
````
/home/linux/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/main/libmain.a(app_main.cpp.obj):(.literal._Z8run_demov+0x14): undefined reference to `c_app_main()'
/home/linux/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: esp-idf/main/libmain.a(app_main.cpp.obj): in function `run_demo()':
/home/linux/.espressif/tools/xtensa-esp32-elf/esp-2020r3-8.4.0/xtensa-esp32-elf/xtensa-esp32-elf/include/c++/8.4.0/bits/stl_vector.h:567: undefined reference to `c_app_main()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/app-template.elf.dir/build.make:508: app-template.elf] Error 1
make[1]: *** [CMakeFiles/Makefile2:1767: CMakeFiles/app-template.elf.dir/all] Error 2
make: *** [Makefile:136: all] Error 2
make failed with exit code 2
````

Can someone suggest what to do, It would be of great help
Thanks in advance

chegewara
Posts: 2306
Joined: Wed Jun 14, 2017 9:00 pm

Re: How do I merge 2 codes with "app_main" and one .c and other .cpp

Postby chegewara » Mon May 24, 2021 1:41 pm

https://isocpp.org/wiki/faq/mixing-c-and-cpp

Also remember to update CMakeLists.txt.

simonjo
Posts: 21
Joined: Wed Sep 30, 2020 9:16 am

Re: How do I merge 2 codes with "app_main" and one .c and other .cpp

Postby simonjo » Mon May 24, 2021 3:41 pm

Also remember that you can have just 1 app_main() function, which is the main entry function for your application.

So just updating CMakeLists.txt is not enough, you need to merge the code if multiple app_main() functions exist.

Harshad P
Posts: 7
Joined: Wed Apr 14, 2021 6:27 am

Re: How do I merge 2 codes with "app_main" and one .c and other .cpp

Postby Harshad P » Tue May 25, 2021 10:44 am

Thanks a lot for the replies, I was able to make it up and running.

Who is online

Users browsing this forum: Bing [Bot] and 244 guests