Various methods of Python calling C + +

Source: Internet
Author: User

Various methods of Python calling C + +

2010-12-07 09:59 28433 People read reviews (1) Favorites

Python is an explanatory language, the bottom is implemented in C, so it is very easy to call C with Python, the following summarizes the various methods of invocation, give examples, all the examples are ubuntu9.10, python2.6 under the test.

1. Python calls C (base)

Want to call C functions in Python, like here's fact

#include <Python.h>

int fact (int n)

{

if (n <= 1)

return 1;

Else

return n * fact (n-1);

}

pyobject* wrap_fact (pyobject* self, pyobject* args)

{

int n, result;

if (! Pyarg_parsetuple (args, "i:fact", &n))

return NULL;

result = fact (n);

Return Py_buildvalue ("I", result);

}

Static Pymethoddef examplemethods[] =

{

{"Fact", Wrap_fact, Meth_varargs, "Caculate n!"},

{NULL, NULL}

};

void Initexample ()

{

pyobject* m;

m = Py_initmodule ("Example", examplemethods);

}

Save this piece of code as a wrapper.c and make it into so library,

Gcc-fpic Wrapper.c-o Example.so-shared-i/usr/include/python2.6-i/usr/lib/python2.6/config

Then, in the directory with this so library, go to Python, you can use the following

Import Example

Example.fact (4)

2. Python calls C + + (base)

Call C + + class member functions in Python, such as the fact function in the Testfact class,

#include <Python.h>

Class testfact{

Public

Testfact () {};

~testfact () {};

int fact (int n);

};

int testfact::fact (int n)

{

if (n <= 1)

return 1;

Else

return n * (n-1);

}

int fact (int n)

{

Testfact T;

return T.fact (n);

}

pyobject* wrap_fact (pyobject* self, pyobject* args)

{

int n, result;

if (! Pyarg_parsetuple (args, "i:fact", &n))

return NULL;

result = fact (n);

Return Py_buildvalue ("I", result);

}

Static Pymethoddef examplemethods[] =

{

{"Fact", Wrap_fact, Meth_varargs, "Caculate n!"},

{NULL, NULL}

};

extern "C"//does not cause initexample to be found

void Initexample ()

{

pyobject* m;

m = Py_initmodule ("Example", examplemethods);

}

Save this piece of code as a wrapper.cpp and make it into so library,

g++-fpic Wrapper.cpp-o Example.so-shared-i/usr/include/python2.6-i/usr/lib/python2.6/config

Then, in the directory with this so library, go to Python, you can use the following

Import Example

Example.fact (4)

3. Python calls C + + (Boost.python)

The boost library is a very powerful library in which the Python library can be used to encapsulate C + + being called by Python and is powerful enough not only to encapsulate functions but also to encapsulate classes, class members.

Http://dev.gameres.com/Program/Abstract/Building%20Hybrid%20Systems%20with%20Boost_Python.CHN.by.JERRY.htm

First install Boost.python under Ubuntu, Apt-get installs Libboost-python-dev

#include <boost/python.hpp>

Char const* greet ()

{

Return "Hello, World";

}

Boost_python_module (Hello)

{

Using namespace boost::p Ython;

def ("greet", greet);

}

Save the code as hello.cpp and compile it into so library

g++ Hello.cpp-o Hello.so-shared-i/usr/include/python2.5-i/usr/lib/python2.5/config-lboost_python-gcc42-mt-1_34_1

Here the Python path is set to your Python path, and must be-lboost_python-gcc42-mt-1_34_1, this library name is not necessarily this, go to/user/lib

Then, in the directory with this so library, go to Python, you can use the following

>>> Import Hello

>>> Hello.greet ()

' Hello, World '

4. Python calls C + + (ctypes)

cTYPES is a advanced FFI (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 It is already included.

cTYPES allows to call functions in dlls/shared libraries and have extensive facilities to create, access and manipulate sim Ple and complicated C data types in python-in other words:wrap libraries in pure Python. It is even possible to implement C callback functions in pure Python.

http://python.net/crew/theller/ctypes/

#include <Python.h>

Class testfact{

Public

Testfact () {};

~testfact () {};

int fact (int n);

};

int testfact::fact (int n)

{

if (n <= 1)

return 1;

Else

return n * (n-1);

}

extern "C"

int fact (int n)

{

Testfact T;

return T.fact (n);

}

Save the code as wrapper.cpp do not write Python interface encapsulation, directly compiled into so library,

g++-fpic Wrapper.cpp-o Example.so-shared-i/usr/include/python2.6-i/usr/lib/python2.6/config

To enter Python, you can use the following

>>> Import cTYPES

>>> Pdll = ctypes. Cdll ('/home/ubuntu/tmp/example.so ')

>>> Pdll.fact (4)

12

Various methods of Python calling C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.