c++ for module development

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
elliotwoods
Posts: 23
Joined: Wed Dec 04, 2019 8:11 am

c++ for module development

Post by elliotwoods » Thu Dec 05, 2019 8:19 am

Hey all!
Loving MicroPython, congrats on everything

I'm working on a module to count Encoder values with the ESP32 hardware counters. I have it working in cpp now here:
https://github.com/elliotwoods/ESP32-Qu ... ncoder.cpp


I noticed that if i add a cpp file to my micropython.mk, i get an error (xtensa can't find the file)
I know that ESPIDF will work with cpp since i tested that directly in PlatformIO
It seems to be a limitation of the micropython workflow that it doesn't support cpp files right now

Perhaps something to do with py/py.mk ?

Code: Select all

SRC_MOD += $(patsubst $(USER_C_MODULES)/%.c,%.c,$(SRC_USERMOD))
My plan was to use extern C to bring everything back to the C file, but it seems that I can't have cpp files at all.
Can anybody help / clarify?

Thank you
Elliot



side note:
Hit my head pretty hard over the last 24 hours trying to get a build environment working. Finally got it working last night, and then from this morning somehow how to start from scratch again (been trying with linux subsystem, msys2, minigw, etc)

Hoping the dockerfile approach will become standardised in the future! (i noticed in another thread)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: c++ for module development

Post by jimmo » Fri Dec 06, 2019 3:35 am

Yes, you're exactly right -- the main MicroPython project is entirely C only.

For ESP32 it doesn't use the IDF build system, so even though their Makefiles (and the newer CMake system) support C++, that doesn't affect MicroPython.

That said, there are examples of using C++ with MicroPython, e.g. https://github.com/bbcmicrobit/micropython/

For the most part, other than like you say, careful use of `extern "C"` where necessary, the main thing is updating the Makefiles to also add the relevant rules for your .cc / .cpp files. Plus probably a default rule for %.cc -> %.o. You'll probably want to make sure that CPPFLAGS disables exceptions etc.

User avatar
elliotwoods
Posts: 23
Joined: Wed Dec 04, 2019 8:11 am

Re: c++ for module development

Post by elliotwoods » Tue Jul 07, 2020 12:14 pm

Hey all

I'm still stuck on this and would really appreciate some help.
A few months back when I wanted to get CPP working, I instead worked around it in C only

This time I want to include a CPP library (Tensorflow lite micro) and therefore need to have CPP support

I'm not very familiar with Makefiles, and the micropython makefile is especially complicated.

First I noticed that having in the makefile

Code: Select all

TENSORFLOW_MOD_DIR := $(USERMOD_DIR)
Doesn't work for cpps, as some of the paths become broken, so instead I go with absolute paths
Now I get

Code: Select all

build-GENERIC/../../../modules/tensorflow/tensorflow.cpp:11: *** missing separator.  Stop.
Line 11 reads:

Code: Select all

int test() {
	return 0;
}
I seem to get that error on the first line of code which defines a symbol (i.e. not an include/other) regardless of whatever I put there.

User avatar
elliotwoods
Posts: 23
Joined: Wed Dec 04, 2019 8:11 am

Re: c++ for module development

Post by elliotwoods » Wed Jul 08, 2020 6:06 am

I've also made a request on the GitHub for official support for CPP in user modules:
https://github.com/micropython/micropython/issues/6233

Maybe it's best that I continue this conversation over there instead

User avatar
elliotwoods
Posts: 23
Joined: Wed Dec 04, 2019 8:11 am

Re: c++ for module development

Post by elliotwoods » Fri Jul 10, 2020 2:48 am

Update :
I have got it working now (in a round-about-hacky way)
Notes in the github issue above

Thank you all!

Keja
Posts: 12
Joined: Sat Dec 19, 2020 9:56 am

Re: c++ for module development

Post by Keja » Sat May 01, 2021 2:13 pm

hello,
I'm trying to do something similar also on ESP32 with a mix of cpp cc files.
The link to step4 is dead, could you detail a bit ?
Thank you

Post Reply