Page 1 of 1
C++ native modules for Micropython?
Posted: Fri Apr 15, 2022 4:04 pm
by ardthusiast
Hello,
I have a program written in C++, and I want to be able to expose some of the functions in it to micropython via native modules. I can only find how to do this with C and python code. How would I use C++? (I want to use C++, not C. Just making this extra clear so that I get relevant answers. I already know how it can be done in C, but I don't want to do that.)
Re: C++ native modules for Micropython?
Posted: Sat Apr 16, 2022 2:07 am
by tepalia02
Re: C++ native modules for Micropython?
Posted: Sat Apr 16, 2022 2:09 pm
by ardthusiast
Interesting, but this is Micropython. Since they're written differently I doubt the exact same method would work. I have found
this library which simplifies the process, but my main problem here is compiling the code. I tried converting the features0 example in examples/natmod to C++ and using that as the source code in the provided makefile, but when I ran make, it failed saying that there was no source file provided. I found that this was because of lines 38 and 39 in py/dynruntime.mk:
Code: Select all
SRC_O += $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(filter %.c,$(SRC))))
SRC_MPY += $(addprefix $(BUILD)/, $(patsubst %.py,%.mpy,$(filter %.py,$(SRC))))
which only allow .c and .py as source files. Changing the .c in line 38 to .cpp allowed it to start the compilation process, but it threw an error. This was the output:
Code: Select all
LINK build/features0.o
Traceback (most recent call last):
File "../../mpy/tools/mpy_ld.py", line 1091, in <module>
main()
File "../../mpy/tools/mpy_ld.py", line 1087, in main
do_link(args)
File "../../mpy/tools/mpy_ld.py", line 1043, in do_link
with open(args.qstrs) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'build/features0.config.h'
make: *** [../../mpy/py/dynruntime.mk:145: build/features0.native.mpy] Error 1
but if I put the features0.c file in the directory, it works even though I specified in the makefile SRC=features0.cpp. How do I get it to compile the C++ file to .mpy? Any suggestions?
Edit: I said above:
but if I put the features0.c file in the directory, it works even though I specified in the makefile SRC=features0.cpp.
When features0.c is present in the directory along with features0.cpp and the Makefile, it compiles features0.c INSTEAD of features0.cpp. Obviously, not what I want. I want it to compile features0.cpp.
Re: C++ native modules for Micropython?
Posted: Sun Apr 17, 2022 6:58 am
by stijn
I tried converting the features0 example in examples/natmod
Do you really need native modules? 'user modules' are quite a lot easier to work with, plus have C++ examples: examples/usercmodules/cppexample. And micrpython-wrap also works that way.
Re: C++ native modules for Micropython?
Posted: Sun Apr 17, 2022 2:24 pm
by ardthusiast
That might work, but don't user modules require me to rebuild the firmware every time I edit the code?
Re: C++ native modules for Micropython?
Posted: Mon Apr 18, 2022 7:24 am
by stijn
Yes the firmware in that case contains everything. 'rebuild' is perhaps not the best word for it, IIRC the Makefile is smart enough to figure out that if you only change one source file of the module it's only going to build/link that object file.