Embedding Micropython in a standalone C application (micropython/examples/embedding )

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Tue Aug 06, 2019 2:34 pm

Hi everyone,

I'm trying to compile the
"micropython/examples/embedding" program.
After running the make command I get this error:
"error: unknown type name 'uintptr_t'.
Does anyone know how to solve it?

Enviroment:

PC: Ubuntu 16.04
MicroPython: $ git clone https://github.com/micropython/micropython.git
$ cd micropython && git submodule update --init

User avatar
jimmo
Posts: 892
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by jimmo » Wed Aug 07, 2019 6:05 am

uintptr_t comes from stdint.h

I'm not sure why mphal.h doesn't include stdint.h -- it probably should, otherwise unix_mphal.c needs to. Could you send a PR?

ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Wed Aug 07, 2019 8:18 am

Sorry for the ignorance: what is a PR? What should I do?

User avatar
jimmo
Posts: 892
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by jimmo » Wed Aug 07, 2019 1:27 pm

PR = Pull request. So you can get this fixed permanently.

But to solve the problem you just need to add

Code: Select all

#include <stdint.h>
to the top of mphal.h

ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Thu Aug 08, 2019 7:35 am

Thank you very much Jimmo,

I added #include <stdint.h> to the top of micropython/py/mphal.h and
the old error is disappear but now there is this one:

In file included from ../../py/mphal.h:35:0,
from ../../py/mpprint.c:33:
../../ports/unix/mphalport.h: In function ‘mp_hal_delay_ms’:
../../ports/unix/mphalport.h:52:52: error: implicit declaration of function ‘usleep’ [-Werror=implicit-function-declaration]
static inline void mp_hal_delay_ms(mp_uint_t ms) { usleep((ms) * 1000); }
^
cc1: all warnings being treated as errors
../../py/mkrules.mk:47: recipe for target 'build/py/mpprint.o' failed

Any suggestions ?

ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Thu Aug 08, 2019 10:19 am

Jimmo,
I resolved the second issue: I have changed in Makefile.upylib the CFLAGS option
-std=gnu99 ( the correct option ) to -std=c99 ( the wrong one ).

Now is all ok: the program is compiled.

By thre way, how I do make the 'Pull request' ?

'Pull request' tab --> New pull request ( green tab ) -->
and then ? ( I can't make a small branch to use them for pull rquest )

User avatar
jimmo
Posts: 892
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by jimmo » Thu Aug 08, 2019 11:19 am

ales.coppelli wrote:
Thu Aug 08, 2019 10:19 am
I resolved the second issue: I have changed in Makefile.upylib the CFLAGS option
-std=gnu99 ( the correct option ) to -std=c99 ( the wrong one ).
I don't know why this solved your problem...? usleep() is in unistd.h, which is already included in mphal.h. When I build examples/embedded, I did see the same problem with stdint.h, but I don't see this problem.
ales.coppelli wrote:
Thu Aug 08, 2019 10:19 am
By thre way, how I do make the 'Pull request' ?

'Pull request' tab --> New pull request ( green tab ) -->
and then ? ( I can't make a small branch to use them for pull rquest )
The usual way is to:
1. Fork micropython/micropython in your own github account
2. Add your fork as a remote to your local git repo on your PC
3. Make the changes in a branch, push it to your github fork
4. Make a pull request from your branch.

ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Fri Aug 09, 2019 12:55 pm

jimmo wrote:
Thu Aug 08, 2019 11:19 am
ales.coppelli wrote:
Thu Aug 08, 2019 10:19 am
I resolved the second issue: I have changed in Makefile.upylib the CFLAGS option
-std=gnu99 ( the correct option ) to -std=c99 ( the wrong one ).
I don't know why this solved your problem...? usleep() is in unistd.h, which is already included in mphal.h. When I build examples/embedded, I did see the same problem with stdint.h, but I don't see this problem.

Try this: in your 'Makefile.uplib' change the CFLAGS option from "-std=gnu99" to "-std=c99".
You should to have my second issue.

User avatar
jimmo
Posts: 892
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by jimmo » Fri Aug 09, 2019 1:38 pm

ales.coppelli wrote:
Fri Aug 09, 2019 12:55 pm
Try this: in your 'Makefile.uplib' change the CFLAGS option from "-std=gnu99" to "-std=c99".
You should to have my second issue.
Ah I misunderstood what you were saying before. Why do you need to change it from gnu99 to c99? Are you compiling with something that isn't gcc? If there's a specific reason to use c99 for your code, could you compile libmicropython with gnu99 and your code with c99 (although i think that's exactly what the demo does).

https://stackoverflow.com/a/55860859 has a great explanation of why this doesn't work (summary: usleep is deprecated, you can add -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L to Makefile.upylib to work around it).

But you're going to run into other problems using std=c99 for building libmicropyhton -- e.g. inline assembler.

ales.coppelli
Posts: 12
Joined: Wed Aug 08, 2018 9:15 am

Re: Embedding Micropython in a standalone C application (micropython/examples/embedding )

Post by ales.coppelli » Fri Aug 09, 2019 5:04 pm

jimmo wrote:
Fri Aug 09, 2019 1:38 pm
ales.coppelli wrote:
Fri Aug 09, 2019 12:55 pm
Try this: in your 'Makefile.uplib' change the CFLAGS option from "-std=gnu99" to "-std=c99".
You should to have my second issue.
Ah I misunderstood what you were saying before. Why do you need to change it from gnu99 to c99? Are you compiling with something that isn't gcc? If there's a specific reason to use c99 for your code, could you compile libmicropython with gnu99 and your code with c99 (although i think that's exactly what the demo does).

https://stackoverflow.com/a/55860859 has a great explanation of why this doesn't work (summary: usleep is deprecated, you can add -D_XOPEN_SOURCE=600 -D_POSIX_C_SOURCE=200112L to Makefile.upylib to work around it).

But you're going to run into other problems using std=c99 for building libmicropyhton -- e.g. inline assembler.

Yes, you are right: for my ignorance ( to try resolve my problem ) I changed that option ( gnu99 to c99 ) without fully understanding the consequences. thank you very much for your patience ( and for my bad English :-) )

Post Reply