I can not build an official example using exretn "C", it leads to errors (see attached picture)
I am using VS Code/ ESP-IDF extension/ ESP-IDF example projects/ bluetooth onoff_server.
(should be the same project as here:https://github.com/espressif/esp-idf/tr ... off_server)
I want to build the onoff_server project with a main.cpp file.
The only changes to the example that i have made are:
1) renamed main.c to main.cpp
2) renamed bard.c to board.cpp
3) changed CMakeLists.txt file in tha main directory
from:
Code: Select all
set(srcs "main.c"
"board.c")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS ".")
Code: Select all
set(srcs "main.cpp"
"board.cpp")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS ".")
Code: Select all
extern "C" {
void app_main(void);
}
Could you please let me know how to do it?
ESP_Angus wrote: ↑Sun Jul 19, 2020 11:23 pmHi Halfnium,
Thanks for documenting this. Just so you know, all of ESP-IDF's public headers should already have C++ header guards. If you find a header in ESP-IDF that doesn't have a guard, this is a bug so please report it. On the current ESP-IDF master branch I can change the attached hello_world_main.cpp as follows and it still links and runs correctly:
The only necessary addition is the 'extern "C"' in the declaration of app_main (as this function doesn't have a declaration in any header). I've combined the declaration line with the function definition to save space, but having it separate as you did is also totally fine.Code: Select all
#include <stdio.h> #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_spi_flash.h" extern "C" void app_main(void) { printf("Hello C++ programming world!\n"); ...
If you find somewhere that linking fails on ESP-IDF, please let us know the version and the full linker error (including which symbol(s) fail to link), and we'll fix it.
Angus
PS There also some dedicated C++ examples in ESP-IDF, although we don't have an introductory "hello world" style one: https://github.com/espressif/esp-idf/tr ... amples/cxx