Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mymodule.c error #3

Open
riegaz opened this issue Jun 3, 2017 · 7 comments
Open

Adding mymodule.c error #3

riegaz opened this issue Jun 3, 2017 · 7 comments

Comments

@riegaz
Copy link

riegaz commented Jun 3, 2017

Following the instructions of how to add a module I do get the following error.

CC mymodule.c
mymodule.c:16:5: error: unknown field 'name' specified in initializer
     .name = MP_QSTR_mymodule,
     ^
mymodule.c:16:5: error: initialization makes pointer from integer without a cast [-Werror]
mymodule.c:16:5: error: (near initialization for 'mp_module_mymodule.globals') [-Werror]
cc1: all warnings being treated as errors
../py/mkrules.mk:47: recipe for target 'build/mymodule.o' failed
make: *** [build/mymodule.o] Error 1

@kmgraves22
Copy link

I am getting this same error. Have you found a solution to it yet?

@riegaz
Copy link
Author

riegaz commented Aug 3, 2017

I managed to make it work in the end but I would have to search for the code. I had to switch to C in the end.

For sure you have to remove the ".name" field because the documentation is outdated and they removed this field in the meantime. If this does not help you, let me know, I will search my code.

@kmgraves22
Copy link

kmgraves22 commented Aug 3, 2017

Thanks. I removed the ".name" field and got it to compile and import the module. If you dont mind, Im having trouble actually calling the function defined here in micropython. After I import mymodule.c would calling the hello world function be mymodule.mymodule_hello() ?

MicroPython v1.9.1-185-gaa7be82-dirty on 2017-08-03; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode

import mymodule
mymodule.mymodule_hello()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'module' object has no attribute 'mymodule_hello'

@riegaz
Copy link
Author

riegaz commented Aug 3, 2017

How to call the function depends on how you import your module. Best way is to open the REPL, import your module and check which function calls it offers you after hitting tab. I can not remember anymore but I think I moved from writing modules in C to directly write helper functions in C because the documentation on writing modules for micropython was quite limited. I think I figrued that out while trying to import arrays into my module.

@naums
Copy link
Contributor

naums commented Aug 10, 2017

Yes the .name-property was outdated, the documentation here is updated now.

The important piece of code is here:

STATIC const mp_map_elem_t mymodule_globals_table[] = {
    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_mymodule) },
    { MP_OBJ_NEW_QSTR(MP_QSTR_hello), (mp_obj_t)&mymodule_hello_obj },
};

The global names of your module consists of a string _name_ and a string hello. The _name_ points to your module-structure, the hello-string to your function-object.

The interesting thing here, is that the Python interpreter basically knows only the strings you let him know. It will not know any of the C-function names or variables you declare in your C-files. These names are there for the C-compiler and once you link it all together for the firmware.img-file, these labels are gone.

So the proper way to call your function would be:

import mymodule
mymodule.hello();

Python knows the hello string from the global names table of your module, looks it up and finds a function object, which python then calls.

@anandcpanchal
Copy link

Hi,
I am trying to apply methods mentioned on https://micropython-dev-docs.readthedocs.io/en/latest/adding-module.html for ESP32.
I have been able to import module and see function name in dir(module) but ESP32 restarts as soon as function is called.
I have missed the step which adds code to iROM section as esp32 linker file is bit different than ESP8266. Compilation worked but execution failed.
Any hints to proceed ahead are welcome!
Thanks.

@deshipu
Copy link
Owner

deshipu commented Dec 9, 2018

I think you will have the best chance for help if you ask this on the micropython forum.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants