Page 1 of 2
can not build when using c++
Posted: Thu Jul 16, 2020 11:11 am
by zhenghaku
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:
Re: can not build when using c++
Posted: Fri Jul 17, 2020 8:00 am
by zhenghaku
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"
}
Re: can not build when using c++
Posted: Fri Jul 17, 2020 9:37 am
by chegewara
Or use this block in header:
Code: Select all
#ifdef __cplusplus
extern "C"
{
#endif
...
#ifdef __cplusplus
}
#endif
Re: can not build when using c++
Posted: Sun Jul 19, 2020 5:08 pm
by Halfnium
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.
Re: can not build when using c++
Posted: Sun Jul 19, 2020 11:23 pm
by ESP_Angus
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
Re: can not build when using c++
Posted: Mon Jul 20, 2020 2:26 am
by zhenghaku
Thank you very much fro being really helpful.
Re: can not build when using c++
Posted: Mon Jul 20, 2020 3:35 am
by Halfnium
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.
Re: can not build when using c++
Posted: Mon Jul 20, 2020 6:26 am
by ESP_Angus
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
Re: can not build when using c++
Posted: Wed Jul 22, 2020 3:20 am
by Halfnium
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.
Re: can not build when using c++
Posted: Wed Jul 22, 2020 4:51 am
by ESP_Angus
Hi Halfnium,
Very glad to hear it's all working now.
Angus