Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5043

MicroPython • Re: Mix Python & C without compiling into micropython?

$
0
0
You can create what are called Native C Extension Modules. These are modules which are written in C, compiled to '.mpy' files, which, when loaded to the Pico file system, can be used by MicroPython programs simply by importing those named modules.

There are however limitations with that. The big one is that the C code for such a module must be entirely self-contained, can only call MicroPython firmware functions which MicroPython chooses to expose to '.mpy' files, not arbitrary functions.

In theory you can add to the list of what MicroPython firmware exposes but I have never got that to work and, as it requires a custom MicroPython build for others to make use of such modules, they aren't useful for general distribution, so I haven't pursued that.

Another problems is C code which explicitly or implicitly uses division and/or modulo functions will not compile without a workaround for that, but that's a molehill in the grand scheme of things.

My conclusion is Native C Extension Modules have only limited utility, aren't usually a replacement for Native C Extensions which require rebuilding the MicroPython firmware whenever changed.

Statistics: Posted by hippy — Sat Jul 27, 2024 10:47 am



Viewing all articles
Browse latest Browse all 5043

Trending Articles