can not build when using c++

zhenghaku
Posts: 8
Joined: Thu Jul 16, 2020 11:00 am

can not build when using c++

Postby zhenghaku » Thu Jul 16, 2020 11:11 am

Hi, I am new to ESP32.
I am using PlatformIO and ESP-IDF framework. I tried to use c++ to program my esp32.
I copied the example code here: https://docs.platformio.org/en/latest/t ... lysis.html
And make it c++ by split it into a cpp file and a c file, since there is some type problems I cannot solve.
It compile ok, but when linking, I got:

Code: Select all

collect2.exe: error: ld returned 1 exit status
*** [.pio\build\firebeetle32\firmware.elf] Error 1
Everything was ok when the files were .c files.
The only modification I have done is changing main.c to main.cpp and adding:

Code: Select all

extern "C" {
   void app_main();
}

zhenghaku
Posts: 8
Joined: Thu Jul 16, 2020 11:00 am

Re: can not build when using c++

Postby zhenghaku » Fri Jul 17, 2020 8:00 am

Finally, after hours of researching and trying I figure it out!
If you are using functions in .c file you have to put the #include in the extern "C" block, like:

Code: Select all

extern "C" {
   void app_main();
   #include "startWifi.h"
}

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

Re: can not build when using c++

Postby chegewara » Fri Jul 17, 2020 9:37 am

Or use this block in header:

Code: Select all

#ifdef __cplusplus
extern "C"
{
#endif

...


#ifdef __cplusplus
}
#endif


Halfnium
Posts: 11
Joined: Thu Jul 02, 2020 10:38 pm

Re: can not build when using c++

Postby Halfnium » Sun Jul 19, 2020 5:08 pm

zhenghaku,

Many people (including me) have encountered this problem. Your and chegewara's solutions are correct.

To help others understand, I have created a C++ version of the hello_world C-language example that Espressif provides within the IDF. Please find this attached.

Note that the new source code file is of type .cpp. Be sure that the project's "Makefile" and "CMakeLists.txt" files' contents are consistent with the project and source file names, else the example will not build.

Have a look at the source code. Note that not every #include is subject to the name mangling / decoration problem. "sdkconfig.h" contains no functions to mangle, just definitions of constants. C++ treats "stdio.h" specially.
Attachments
hello_world_main.cpp
(1.36 KiB) Downloaded 580 times

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: can not build when using c++

Postby ESP_Angus » 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

zhenghaku
Posts: 8
Joined: Thu Jul 16, 2020 11:00 am

Re: can not build when using c++

Postby zhenghaku » Mon Jul 20, 2020 2:26 am

Thank you very much fro being really helpful. :D

Halfnium
Posts: 11
Joined: Thu Jul 02, 2020 10:38 pm

Re: can not build when using c++

Postby Halfnium » Mon Jul 20, 2020 3:35 am

ESP_Angus,

Yes, I had noticed those C++ guards when perusing the include files. Nonetheless, Eclipse (2020-06 version) refused to resolve the references until I put them inside the "extern C." Building from Eclipse otherwise failed with 23 unresolved references.

I am also a new guy. I was so fixated on Eclipse that I didn't imagine that this project which would not build on Eclipse might build successfully from the command line. It certainly does! (I just now tried it.)

So perhaps what we really have here is a problem with Eclipse, perhaps involving the IDF plug in.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: can not build when using c++

Postby ESP_Angus » Mon Jul 20, 2020 6:26 am

Hi Halfnium,

Thanks for the additional explanation! Agree it does sound like an issue of some kind in Eclipse. If you're using the IDF Eclipse Plugin then could you open an issue here, please? https://github.com/espressif/idf-eclipse-plugin/issues/ with as many details as possible.

(Because Eclipse keeps its own internal model of the source code for the editor windows, it's possible to be in the unusual situation where there are "errors" shown in the code editor but the project is building!)

Angus

Halfnium
Posts: 11
Joined: Thu Jul 02, 2020 10:38 pm

Re: can not build when using c++

Postby Halfnium » Wed Jul 22, 2020 3:20 am

Angus,

I may have to eat my hat. I set out to reproduce the problem as described earlier in this thread but have not been able to make it happen again: the declaration of main() as extern "C" is sufficient. Maybe this is something subtle that only happens as part of the newbie workflow. Or maybe I managed to conflate two problems, just like a typical newbie. Confusing.

Anyway, I have no issue to report for the Eclipse Plugin at this moment.

ESP_Angus
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: can not build when using c++

Postby ESP_Angus » Wed Jul 22, 2020 4:51 am

Hi Halfnium,

Very glad to hear it's all working now.

Angus

Who is online

Users browsing this forum: Baidu [Spider] and 132 guests