Python C interoperability

。_饼干妹妹 提交于 2019-12-07 12:49:50

问题


I wish to wrap an existing C (pure C that is. No C++) library into Python so that I can call it from Python scripts. Which approach among the various available (C Api, SWIG etc.) would be the most suitable?


回答1:


go with Ctypes, it is part of standard distribution and works very well. basically you can wrap C structures and types in python classes, as well as functions. Some types and functionality is already provided by library.

ctypes

couple caveats though: passing triple pointers to C routines is not obvious (if you have to), and I could not get it to work with static libraries on Linux, DLL and shared objects are fine.




回答2:


SWIG is great for doing this. Here is a sample tutorial: http://www.swig.org/Doc1.3/Python.html.




回答3:


Since your code is "pure" C you might consider using Pyrex/Cython. This is not a voting issue and Cython has already been mentioned. I am just clarifying why it is a better choice for pure C.



来源:https://stackoverflow.com/questions/2144542/python-c-interoperability

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!