C++ native modules for Micropython?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

C++ native modules for Micropython?

Post by ardthusiast » Fri Apr 15, 2022 4:04 pm

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.)

tepalia02
Posts: 99
Joined: Mon Mar 21, 2022 5:13 am

Re: C++ native modules for Micropython?

Post by tepalia02 » Sat Apr 16, 2022 2:07 am

Hi, have you seen this thread? Was it of any help?
https://stackoverflow.com/questions/905 ... ython-in-c

User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Re: C++ native modules for Micropython?

Post by ardthusiast » Sat Apr 16, 2022 2:09 pm

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.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: C++ native modules for Micropython?

Post by stijn » Sun Apr 17, 2022 6:58 am

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.

User avatar
ardthusiast
Posts: 25
Joined: Tue Feb 08, 2022 4:13 pm

Re: C++ native modules for Micropython?

Post by ardthusiast » Sun Apr 17, 2022 2:24 pm

That might work, but don't user modules require me to rebuild the firmware every time I edit the code?

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: C++ native modules for Micropython?

Post by stijn » Mon Apr 18, 2022 7:24 am

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.

Post Reply