can not build when using c++

bigdanv
Posts: 1
Joined: Thu Apr 15, 2021 2:39 pm

Re: can not build when using c++

Postby bigdanv » Wed Apr 28, 2021 3:12 pm

Hello Angus,

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  ".")
to:

Code: Select all

set(srcs "main.cpp"
    "board.cpp")

idf_component_register(SRCS "${srcs}"
                    INCLUDE_DIRS  ".")
4) added following lines to the main.cpp file:

Code: Select all

extern "C" {
   void app_main(void);
}
Is it possible to build the project with main.cpp file?
Could you please let me know how to do it?

ESP_Angus wrote:
Sun Jul 19, 2020 11:23 pm
Hi 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:

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

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
Attachments
onoff_server.png
onoff_server.png (112.35 KiB) Viewed 1903 times

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

Re: can not build when using c++

Postby chegewara » Wed Apr 28, 2021 6:08 pm

Most errors are self explaining. In gpio related functions you cant pass a number as a pin, you have to use GPIO_NUM_X.

Same about esp_ble_mesh_model_pub_t::msg. In C you can mix variables order in struct when you init it, in C++ order has to be exact the same as in struct definition.

Who is online

Users browsing this forum: egionet and 327 guests