Boost Python Library

Vai alla fonte

Boost Python Library è un potente Framework che ci permette di ‘unire’ (o meglio implementare in…) C++ e Python; è molto semplice e immediato integrare classi e funzioni di un codice C++ con gli oggetti e le funzioni di Python.

Dal sito ufficiale:

“The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools — just your C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it, making Boost.Python ideal for exposing 3rd-party libraries to Python. The library’s use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative interface definition language (IDL).”

Un rapido esempio? Certo:

Proviamo a scrivere un semplicissimo Hello World, in C++:

char const* greet()

{

     return “Hello, World!”;

}

Molto semplice, d’altronde è C++.

Ora procediamo, e scriviamo il nostro piccolo script Python:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)

{

       usin namespace boost::python;
       def(”greet”, greet);

}

Apriamo il nostro interprete Python, e…. sorpresa!

>>> import hello_ext
>>> print hello.greet()
Hello, World! 

It’s complete! ;)

articoli correlati

Commenta

Articoli Correlati