C++

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Aris
Posts: 14
Joined: Wed Feb 25, 2015 1:00 pm

C++

Post by Aris » Wed Jul 01, 2015 9:45 am

Hi everyone,

Does anyone know if there is a possibility to use C with pyboard?

Thanks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: C++

Post by dhylands » Wed Jul 01, 2015 3:23 pm

I don't see why it shouldn't be possible.

I presume you meant to ask about C++ since that's what the title says.

Currently micropython doesn't setup all of the environment needed to do C++ (like exception handling), but if you did that then things should work.

I guess it depends on what you're really asking.

Aris
Posts: 14
Joined: Wed Feb 25, 2015 1:00 pm

Re: C++

Post by Aris » Mon Jul 06, 2015 2:03 pm

Thanks for the reply.

I've been asked to make C++ (and C) work on the pyboard. I've not been able to make it so far.. Actually I don't have a clue of how could I make it. I must mention that I've never studied C nor C++.. nor any other language except Python (and just a little bit).

That's why I'm asking for a little explanation of how could I make it.

I found this link https://github.com/stinos/micropython-wrap but I don't understand exactly what it does nor how to make it work.

Thank you!

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

Re: C++

Post by stijn » Mon Jul 06, 2015 2:46 pm

Aris wrote:to make C++ (and C) work on the pyboard
You'll have to be *way* more specific as to what that means. Do you have a concrete example?

Aris
Posts: 14
Joined: Wed Feb 25, 2015 1:00 pm

Re: C++

Post by Aris » Mon Jul 06, 2015 7:26 pm

I want to know if there is a possibility to write C++ code in the main file of my pyboard, something like the "inline assembler" (available in micropython) with C++ instead. Or maybe if it's possible to compile some program, functions or classes in C++ and import them to micropython.

This https://github.com/stinos/micropython-wrap shows how to do it, doesn't it? The problem is that I don't understand exactly how to use it. There is a Usage Sample, but I don't know what to do with the whole code. Should I compile it? and then what else? If you open the link attached, it says:
After calling RegisterMyModule from uPy's main for instance, module mod can be used in Python like this:
How can I call RegisterMyModule from uPy's main? What is "RegisterMyModule"?

I'm sorry if I'm not doing the right questions. I just want to know if it's possible to use C++, and make a little "tutorial" about it for non competent people like me.

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

Re: C++

Post by stijn » Tue Jul 07, 2015 10:45 am

If you are going to use plain C, you could just look in the extmod directory and see how it's done there. Those files are perfect examples of adding your own functions/classes to uPy. You just have to add the file you want to compile to the SRC_C list in the Makefile.
Aris wrote:This https://github.com/stinos/micropython-wrap shows how to do it, doesn't it?
Yes, I wrote that stuff. Only tested on unix and windows though and it takes a bunch of modifications to the micropython source to get it working, mainly to get C++ compiled. It has been a while I did it on unix but I checked again and put the modifications in a branch which you can find here: https://github.com/micropython/micropyt ... rap-sample. Clone this branch, cd unix, make. If you apply similar changes to the pyboard Makefile it probably works there as well.

Note that there's quite a lot of changes, so if you plan to use this you are going to have to spend time keeping these changes in sync with the latest uPy code (or get some parts merged into uPy itself). So you'll have to outweigh some pros and cons: if this is just to play around with custom code which can be done in C I would not recommend micropython-wrap. If you have however tons of C++ code, don't want to bother with manual argument parsing/conversion, and don't mind having to rebase the makefile changes against the uPy master branch or work to get them to be part of uPy then it will save you time.

Anyway, the sample defines a simple C++ class and function and registers them in a module called "mod"
As such when building and running you get this sample output:

Code: Select all

Micro Python v1.4.3-242-gc91727b-dirty on 2015-07-07; linux version
>>> import mod
>>> mod.SomeFunc()
35
>>> a=mod.SomeClass()
>>> a.Concat('a',0)
'a0'

Aris
Posts: 14
Joined: Wed Feb 25, 2015 1:00 pm

Re: C++

Post by Aris » Fri Jul 17, 2015 8:51 am

Thank you very much! this helped me a lot.

Post Reply