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.)
C++ native modules for Micropython?
Re: C++ native modules for Micropython?
Hi, have you seen this thread? Was it of any help?
https://stackoverflow.com/questions/905 ... ython-in-c
https://stackoverflow.com/questions/905 ... ython-in-c
- ardthusiast
- Posts: 25
- Joined: Tue Feb 08, 2022 4:13 pm
Re: C++ native modules for Micropython?
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:
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:
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:
Code: Select all
SRC_O += $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(filter %.c,$(SRC))))
SRC_MPY += $(addprefix $(BUILD)/, $(patsubst %.py,%.mpy,$(filter %.py,$(SRC))))
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
Edit: I said above:
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.but if I put the features0.c file in the directory, it works even though I specified in the makefile SRC=features0.cpp.
Re: C++ native modules for Micropython?
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.I tried converting the features0 example in examples/natmod
- ardthusiast
- Posts: 25
- Joined: Tue Feb 08, 2022 4:13 pm
Re: C++ native modules for Micropython?
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?
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.