Page 1 of 1

c++ projects in IDF?

Posted: Sun Dec 05, 2021 3:15 am
by jimcondit
Is there an easy way to create a new cpp project in the idf? I'm trying to migrate a sketch from Android IDE, which is generally working there, to IDF. I can see some reference to using the naming convention "main/main.cpp" and changes to the make files, but I can't make it work. Any documentation?

Re: c++ projects in IDF?

Posted: Mon Dec 06, 2021 3:25 pm
by silardgal_dotlab
Just use

Code: Select all

extern "C" void app_main()
and rename your main.c to main.cpp :D

Re: c++ projects in IDF?

Posted: Mon Dec 06, 2021 4:24 pm
by StuartLittle57
app_main() is called by C code. If you compile your main file as cpp this function name will be mangled. The solution is to define app_main as extern "C" so it can be called from C. That's all. We use C++ in all our projects.

Code: Select all

extern "C" void app_main(void)
{
}
Just note that exceptions are disabled by default. You can enable them in menuconfig.
Also if you want to use modern C++ features you can set C++ version to C++17 in CMakeLists.txt:

Code: Select all

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)