Rahul
Rahul

Reputation: 11679

how to develop a python wrapper for a C code?

Well 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 ?

Upvotes: 0

Views: 184

Answers (2)

The Archetypal Paul
The Archetypal Paul

Reputation: 41779

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

Upvotes: 1

Eli Bendersky
Eli Bendersky

Reputation: 273844

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.

Upvotes: 2

Related Questions