c++ for module development

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Posts: 8
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

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
Posts: 1465
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

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.

Post Reply