开发者

how to develop a python wrapper for a C code?

Well开发者_如何学JAVA given a C code , is there a way that i can use other languages like python to execute the C code . What i am trying to say is , there are soo many modules which are built using a language , but also offer access via different languages , is there any way to do that ?


Of course, it's called "extending" in the Python world. The official documentation is here. A short excerpt:

This document describes how to write modules in C or C++ to extend the Python interpreter with new modules. Those modules can define new functions but also new object types and their methods. The document also describes how to embed the Python interpreter in another application, for use as an extension language. Finally, it shows how to compile and link extension modules so that they can be loaded dynamically (at run time) into the interpreter, if the underlying operating system supports this feature.

An even easier way for Python would be using the ctypes standard package to run code in DLLs.


Many ways. Generically, this is often called a Foreign Function Interface. That Wikipedia page says the following about Python:

* The major dynamic languages, such as Python, Perl, Tcl, and Ruby,

all provide easy access to native code written in C/C++ (or any other language obeying C/C++ calling conventions). o Python additionally provides the Ctypes module 2, which can load C functions from shared libraries/DLLs on-the-fly and translate simple data types automatically between Python and C semantics. For example:

 import ctypes libc = ctypes.CDLL('/lib/libc.so.6' )   # under Linux/Unix
 t = libc.time(None) # equivalent C code: t = time(NULL)
 print t

A popular choice that supports many languages is SWIG

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜