Visual Studio Code, ESP-IDF, C++ include errors?

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Visual Studio Code, ESP-IDF, C++ include errors?

Postby nockieboy » Mon Sep 24, 2018 7:49 am

Hi everyone,

I've recently started playing with the ESP32 using the Arduino IDE, but have now tried to swap to the ESP-IDF and Visual Studio Code tool chain so I've got more control. I'm just starting out with C++, but have experience with other languages.

I've successfully installed the ESP tool chain, msys32 and mingw32 seem to be working okay - I can compile and flash the example 'hello world' program to the ESP32 from the mingw32 command line.

I've followed guides on setting up Visual Studio Code to work with the ESP-IDE and can compile and flash the 'hello world' program to the ESP32 from VS Code successfully.

However, I've been going round in circles trying to get access to the C++ string library. I need it to progress my program for the ESP32, which uses http_server, to analyse and act upon queries passes in the URL.

I've included string:

Code: Select all

#include <stdio.h>
#include <string.h>
And have tried accessing it like this:

Code: Select all

if (std::to_string(var) == "text") { blah }
and

Code: Select all

std::string text = "Hello!";
But I've been getting errors - VS Code couldn't find the include originally (for string), so I tried including string.h, but that had the same error. I've searched the net, found quite a few posts about this issue, but none of the solutions have worked for me. As a result of trying out various solutions, it seems IntelliSense is now picking up <string.h> and <string>, I'm able to r/click on the include and go to the definition, but when I try to build the code ('make' in the mingw32 terminal), I'm getting this error:

Code: Select all

$ make
Python requirements from C:/msys32/home/user/esp-idf/requirements.txt are satisfied.
CC build/main/main.o
C:/msys32/home/user/esp-test/main/main.c: In function 'gpio_get_handler':
C:/msys32/home/user/esp-test/main/main.c:164:10: error: 'std' undeclared (first use in this function)
      if (std::to_string(GPIOs[n]) == param) {
          ^
C:/msys32/home/user/esp-test/main/main.c:164:10: note: each undeclared identifier is reported only once for each function it appears in
C:/msys32/home/user/esp-test/main/main.c:164:13: error: expected ')' before ':' token
      if (std::to_string(GPIOs[n]) == param) {
             ^
make[1]: *** [/home/user/esp-idf/make/component_wrapper.mk:286: main.o] Error 1
make: *** [C:/msys32/home/user/esp-idf/make/project.mk:481: component-main-build] Error 2
I've had pretty much the same issues with Eclipse as well - could compile example programs, but nothing that tried to use string.h. I've since given up on Eclipse - seems massively complicated compared to VS Code for no real gain, so I'm focusing on getting VS Code working properly for now.

Seems to me there's an issue with my C++ installation or path... but everything else compiles okay. Any ideas? :?

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

Re: Visual Studio Code, ESP-IDF, C++ include errors?

Postby ESP_Angus » Mon Sep 24, 2018 11:34 pm

The ESP-IDF build system will compile .c files as C, not C++. To use C++ features, rename the file to .cpp.

(Note that in C++ you'll also have to declare the app_main function as "extern "C" void app_main() { ... " so it gets linked correctly into the app.

nockieboy
Posts: 13
Joined: Fri Sep 21, 2018 1:21 pm

Re: Visual Studio Code, ESP-IDF, C++ include errors?

Postby nockieboy » Tue Sep 25, 2018 7:45 am

ESP_Angus wrote:The ESP-IDF build system will compile .c files as C, not C++. To use C++ features, rename the file to .cpp.

(Note that in C++ you'll also have to declare the app_main function as "extern "C" void app_main() { ... " so it gets linked correctly into the app.
Thanks for your help, Angus. :D Yes, it's definitely because I've been getting confused between C and C++ in the project. As soon as I realised it was actually a C project, I managed to get it working - although I'd much rather use C++ as it seems easier to handle strings etc. Anyway, I've renamed `main.c` to `main.cpp`, and changed `app_main` to `extern "C" void app_main()` (hopefully that's right?) but I'm getting an error thrown by make now:

Code: Select all

make[1]: *** No rule to make target 'C:/msys32/home/user/esp-test2/main/main.c', needed by 'main.o'.  Stop.
make: *** [C:/msys32/home/user/esp-idf/make/project.mk:481: component-main-build] Error 2
I'm not familiar enough with make, the build process, or the Linux CLI in general :shock:, so if you're able to offer any guidance on switching the project over to C++ (or point me in the right direction) I'd appreciate it!

User avatar
gunar.kroeger
Posts: 143
Joined: Fri Jul 27, 2018 6:48 pm

Re: Visual Studio Code, ESP-IDF, C++ include errors?

Postby gunar.kroeger » Mon Oct 08, 2018 4:42 pm

I had the same issue.
If you clean the project and rebuild it it should work.
It seams that changing the extension from .c to .cpp breaks things a bit
"Running was invented in 1612 by Thomas Running when he tried to walk twice at the same time."

Who is online

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