C++ set-up question

_Phil_
Posts: 2
Joined: Thu Nov 09, 2017 9:43 am

C++ set-up question

Postby _Phil_ » Thu Nov 09, 2017 11:12 am

Hi all,

I'm just starting to dig into ESP-IDF and I need to port some C++ classes over from my old Arduino platform, but I've hit what I suspect is a really stupid problem on my part.

I've been though the details in the following post https://esp32.com/viewtopic.php?t=362 and I've also got a copy of Neil Kolban's ESP32 book which I'm using as a guide for C++ integration.

Here's how I've got to this point.

Created a new proeject based on the details in Neils book by git'ing the esp-idf-template and ran make menuconfig and make - which results in a clean compile.

Added the triangle cpp and h files from bengchet's zip into the main directory

Added '

Code: Select all

COMPONENT_ADD_LDFLAGS=-lstdc++ -l$(COMPONENT_NAME)
' to the component.mk in the same dir.

Compile works fine.

Replaced main.c with bengchet's fixed code from the forum post and it blows chunks.

______________________________________________________
CC build/main/main.o
In file included from C:/msys32/home/Phil/esp/myapp/main/main.c:8:0:
C:/msys32/home/Phil/esp/myapp/main/triangle.h:11:1: error: unknown type name 'class'
class Triangle{
^
C:/msys32/home/Phil/esp/myapp/main/triangle.h:11:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token
class Triangle{
^
C:/msys32/home/Phil/esp/myapp/main/main.c:10:1: error: unknown type name 'Triangle'
Triangle polygon(3, 4, 5); //<--- example class
^
C:/msys32/home/Phil/esp/myapp/main/main.c:10:18: error: expected declaration specifiers or '...' before numeric constant
Triangle polygon(3, 4, 5); //<--- example class
^
C:/msys32/home/Phil/esp/myapp/main/main.c:10:21: error: expected declaration specifiers or '...' before numeric constant
Triangle polygon(3, 4, 5); //<--- example class
^
C:/msys32/home/Phil/esp/myapp/main/main.c:10:24: error: expected declaration specifiers or '...' before numeric constant
Triangle polygon(3, 4, 5); //<--- example class
^
C:/msys32/home/Phil/esp/myapp/main/main.c:17:8: error: expected identifier or '(' before string constant
extern "C" int app_main(void)
^
make[1]: *** [/home/Phil/esp/esp-idf/make/component_wrapper.mk:243: main.o] Error 1
make: *** [C:/msys32/home/Phil/esp/esp-idf/make/project.mk:435: component-main-build] Error 2

______________________________________________________

I've also had the same issues when I use Neil's MyClass C++ example from his book (fixing the myStaticFunc issue) with a fresh esp-idf-templated app, it seems to fail with the class definition the same as with the triangle class.

Halp!

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: C++ set-up question

Postby kolban » Fri Nov 10, 2017 5:56 am

Howdy,
Welcome to the forum and the community. There are tons of great folks here to assist.

Thankfully, I think this problem might be quite easy (that isn't normally the case).

If you source code is C++ then the file extension (suffix) must be ".cpp". My recommendation is to rename your "main.c" file to be "main.cpp" and then delete the "build" directory and recompile. While one shouldn't need to delete the build directory, it does no harm as all will be rebuilt ... however, ive seen "strangeness" when one changes the file suffix of a file that was previously compiled. Try this and post back with the results.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

f.h-f.s.
Posts: 214
Joined: Thu Dec 08, 2016 2:53 pm

Re: C++ set-up question

Postby f.h-f.s. » Fri Nov 10, 2017 10:23 am

Doesn't have to be cpp. see this discussion: https://stackoverflow.com/questions/154 ... -cc-vs-cpp
The hint in the error would be "CC build/main/main.o" CC means gcc was used, not "xtensa-esp32-elf-g++".
When you change your main to cpp you will see it compile as "CXX build/main/main.o"

_Phil_
Posts: 2
Joined: Thu Nov 09, 2017 9:43 am

Re: C++ set-up question

Postby _Phil_ » Fri Nov 10, 2017 10:46 am

Hi all,

Yep, it was my stupid error (hides in shame self flagelating).

Following the details I posted above, with Neil's suggestion to remove the build/ dir, and just renaming main.c to main.cc it compiled fine (after taking out the wifi code as it was throwing errors with the sprintf).

Here's the start of the compile showing it's picking up the right compiler now.

Code: Select all

$ make
CXX build/main/main.o
CXX build/main/triangle.o
Thanks guys.

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 113 guests