C Modules compiled as C++

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Posts: 2
Joined: Fri Mar 20, 2020 5:46 pm

C Modules compiled as C++

Post by bluefarmer » Mon Mar 23, 2020 2:13 pm


I've got a bunch of code and utilities that I'd like to tap into from MicroPython. This functionality is accessed through some c++ header files.
Unfortunately, by default the C Modules (like the name) are C only. I could create an intermediary layer that would wrap a c++ function util::math::add(int,int) to util_math_add(int, int) but that add development and runtime overhead and doesn't allow me to use class objects in the C modules.

An ideal solution would be to have the C modules be compiled as C++ and for certain functions to be made C compatible by using extern "C".
I've given this a try by using extern "C" and changing the makefile to compile with g++ however the makefiles don't seem to be designed for this.

Has anyone given this a try before or knows if it's possible or not?


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

Re: C Modules compiled as C++

Post by jimmo » Mon Mar 23, 2020 10:14 pm

Yes you'd need to do a bit of Makefile plumbing to make it compile C++ code. It's not impossible, especially if you confine it to User C Modules, but I can see it getting a bit messy with QSTR extraction etc.

I wonder if rather than wrapping your utils, instead write up all your module functionality standalone, link it into a .a file, then provide a minimal C interface to it that you can call from the C module, and then you only need to worry about adding that .a file to the MicroPython linker stage.

i.e. the .c user module just provides the types and the method dicts, and each method just calls a function directly from the header file for your .a. Inside the .cc/.cpp files you can extern C the MicroPython headers.

It's the same as your first suggestion, but the boundary is at the module functionality rather than the utilities, and the build is self contained.

(That's a bit of a half-baked idea and I'm sure there are lots of issues, and if it were me I'd probably try and make C++ work properly in the Makefiles... although getting that merged upstream might be a challenge? don't know!)

Posts: 426
Joined: Thu Apr 24, 2014 9:13 am

Re: C Modules compiled as C++

Post by stijn » Tue Mar 24, 2020 7:03 am

What Jimmo describes works, I've used things like that.

You could also do (almost) everything in C++ including defining types etc, and then the .c module file just needs to forward a single call to populate the module globals to C++. A bit of a hack, but just to show there's more than one possibility. See https://github.com/stinos/micropython-w ... s/module.c.

I've also tinkered with mixing C and C++ within MicroPython by having the Makefile select compiler based on file extension or so, and that alo worked but seems like I don't have that code anymore, sorry :cry:

Posts: 2
Joined: Fri Mar 20, 2020 5:46 pm

Re: C Modules compiled as C++

Post by bluefarmer » Fri Mar 27, 2020 2:14 pm

Great, thanks for the suggestions! This gives me some directions to try.

Post Reply