Python Forum
generating ctypes wrapper for a c library? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: generating ctypes wrapper for a c library? (/thread-13840.html)



generating ctypes wrapper for a c library? - brighteningeyes - Nov-03-2018

hi,
i want to wrap a C header into ctypes
but, as the header file is large, (lots of structs, callbacks etc), i don't want to put most of my effort on writing the ctypes wrapper by hand
is there any way of parsing the header and generate the ctypes wrapper?
i have seen pycparser, but i don't know how to generate ctypes wrappers with it.
thanks


RE: generating ctypes wrapper for a c library? - Larz60+ - Nov-03-2018

python has ctypes built in:
import ctypes



RE: generating ctypes wrapper for a c library? - brighteningeyes - Nov-03-2018

hi again,
i know ctypes can be imported!.
but the problem is interfacing the module with it. (like the structs, enums, #defines, unions, pointers to functions etc)
for example:
from ctypes import *
class vector3f(Structure):
    _fields_ = [
("x", c_float),
("y", c_float),
("z", c_float)
]
this was a simple structure
but, i need to generate them automatically. not writing everything by hand.


RE: generating ctypes wrapper for a c library? - wavic - Nov-03-2018

Does this help?: https://docs.python.org/3.7/extending/extending.html


RE: generating ctypes wrapper for a c library? - brighteningeyes - Nov-03-2018

since my library has pointers to functions, this can't help me. otherwise i would have written a swig wrapper for it!.


RE: generating ctypes wrapper for a c library? - Gribouillis - Nov-03-2018

Obviously people thought of using pycparser for generating ctypes wrappers. A search for the keywords pycparser ctypes yields this module among other results. Could it be what you're looking for?


RE: generating ctypes wrapper for a c library? - brighteningeyes - Nov-03-2018

somehow yes.
but i've tried using it, but gives errors like cannot import pipeline
first printer_python, which i've copied that into my site-packages directory, then pipeline which i couldn't install from pip and i don't know where should i get that.


RE: generating ctypes wrapper for a c library? - Gribouillis - Nov-03-2018

(Nov-03-2018, 04:01 PM)brighteningeyes Wrote: which i've copied that into my site-packages directory
This is not the correct way to install python modules. Have you tried
Output:
python3 -m pip install ctypesgen



RE: generating ctypes wrapper for a c library? - brighteningeyes - Nov-03-2018

hi again,
i didn't know that this module is also available on pip
first i got ctypesgen from gitHub and tried running it's setup.py install
the error of ModuleNotFoundError: could not find pipeline was fixed because i have mistakenly copied ctypesgen's processor into my site-packages directory replacing the processor module
now, the error that i got:
Error:
Collecting ctypesgen Using cached https://files.pythonhosted.org/packages/b8/9d/13bcf53a190d2a5b3512d3c116920b4a2fadf007600722114b1a17602524/ctypesgen-0.r125.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 1, in <module> File "C:\Users\Amir\AppData\Local\Temp\pip-install-y0k3z6ra\ctypesgen\setup.py", line 13, in <module> import ctypesgencore File "C:\Users\Amir\AppData\Local\Temp\pip-install-y0k3z6ra\ctypesgen\ctypesgencore\__init__.py", line 60, in <module> import descriptions ModuleNotFoundError: No module named 'descriptions' ----------------------------------------
by trying pip install descriptions, i couldn't find it on pip.
thanks


RE: generating ctypes wrapper for a c library? - brighteningeyes - Nov-04-2018

it seems that ctypesgen or processor is available for python2 (not python3) although in the processor's pip page, it said in it's classifiers that it is available for python3