Boost.Python


Boost è una libreria C++. Boost.Python è una libreria C++ che permette di scrivere moduli in C++/Python da usare in Python (usando semplicemente import modulo_creato).

In questo mini-howto proverò a spiegare come installare Boost.Python (usando i pacchetti .deb) su Debian.

Per usare Boost hai bisogno di alcuni pacchetti. I più importanti sono:

{i} Suggerimento:
Per installare Boost sul proprio sistema operativo consiglio di compilare i sorgenti, così da evitare alcuni problemi. Per installare i sorgenti andare qui per la spiegazione. Comunque questo howto non copre la spiegazione dell'installazione da sorgenti.

Installare su Debian

Sui sistemi Debian potete usare apt-get per installare Boost:

# apt-get install libboost-dev bjam boost-build

{i} Informazione
Su Debian Lenny libboost-dev è alla versione 1.34.1, ma tranquilli, il procedimento è lo stesso per la versione 1.35.

apt-get chiederà di installare alcuni pacchetti (libboost-dev, libboost-doc, libboost-python, etc...). Installateli e provate a fare alcuni test per verificare che l'installazione sia andata a buon fine. Se i vostri test non dovessero funzionare, allora seguite il tutorial.

Configurazione

Entrare nella directory /usr/share/boost-build e aprire il file user-config.jam. Quando trovate queste linee:

#  Configure gcc (default version)
#  using gcc;

Decommentare using gcc e aggiungere queste linee alla fine:

  # Configure Python 
  using python : 2.5 : /usr/bin/python2.5 ;

{i} Informazione
Invece di 2.5 potete usare anche 2.4 o 2.3. Comunque potete usare lo stesso metodo per definire gcc ( per esempio "using gcc : 4.3 ;"). Boost.Python richiede Python 2.2 o superiore.

Copiate il file user-config.jam nella directory della vostra home.

$ cp /usr/share/boost-build/user-config.jam ~/

Eseguire bjam

Ora entrate nella directory /usr/share/doc/libboost-doc/examples/libs/python/example/tutorial e digitate:

bjam 

Vi dà un errore come questo?

/usr/share/boost-build/build/project.jam:270: in find-jamfile from module project
error: Unable to load Jamfile.
error: Could not find a Jamfile in directory '../../../..'.
error: Attempted to find it with pattern '[Bb]uild.jam [Jj]amfile.v2 [Jj]amfile [Jj]amfile.jam'.
error: Please consult the documentation at 'http://www.boost.org'.
....

Bene, dobbiamo correggere questo (maledetto!!) errore che non permetterà di compilare niente! Usate una directory per effettuare test di controllo, per esempio copiate l'intera directory e incollatela nella vostra home:

$ cp -r /usr/share/doc/libboost-doc/ ~/

Dovrete aspettare qualche secondo perchè libboost-doc è di circa 55.5 MB. Ora in una console digitate questi comandi:

$ cd ~/libboost-doc/examples
touch project-root.jam  # QUESTO COMANDO È IL PIÙ IMPORTANTE!!
$ cd ~/libboost-doc/examples/libs/python/example

e modificate boost-build.jam: commentate la riga "boost-build ../../../tools/build/v2 ;" e aggiungete questa "boost-build /usr/share/boost-build/kernel ;". <br>

Nella stessa directory modificate il file Jamroot. Quando trovate qualcosa com questo:

# Set up the project-wide requirements that everything uses the
# boost_python library from the project whose global ID is
# /boost/python.
project : requirements <library>/boost/python//boost_python ;

Cambiatelo in:

project : requirements <library>/usr/lib/libboost_python.so
<include>/usr/include/python2.5 ; 
# DOVETE MODIFICARE python2.5 nella vostra versione di Python

Ora entrate nella directory tutorial e modificate il file Jamroot allo STESSO modo di prima.

Ora eseguite bjam. Dovrebbe stampare:

...found 11 targets...
...updating 5 targets...
MkDir1 bin
MkDir1 bin/gcc-4.3
MkDir1 bin/gcc-4.3/debug
gcc.compile.c++ bin/gcc-4.3/debug/hello.o
gcc.link.dll bin/gcc-4.3/debug/hello.so
...updated 5 targets...

Entrate nella directory appena creata (bin/gcc-4.3/debug nel mio esempio) e eseguite l'interprete Python:

   1 >>> import hello
   2 >>> hello.greet()
   3 'hello, w orld'

Ora fate la stessa cosa per il progetto quickstart nella directory ~/libboost-doc/examples/libs/python/example. Modificate boost-build.jam e Jamroot come mostrato sopra, eseguite "gunzip" (per decomprimere) su embedding.cpp.gz e eseguite bjam:

...found 20 targets...
...updating 7 targets...
MkDir1 bin
MkDir1 bin/gcc-4.3
MkDir1 bin/gcc-4.3/debug
gcc.compile.c++ bin/gcc-4.3/debug/extending.o
gcc.link.dll bin/gcc-4.3/debug/extending.so
gcc.compile.c++ bin/gcc-4.3/debug/embedding.o
embedding.cpp: In function ‘void exec_test()’:
embedding.cpp:56: warning: deprecated conversion from string constant to ‘char*’
gcc.link bin/gcc-4.3/debug/embedding
...updated 7 targets...

Ora entrate nella directory bin/gcc-4.3/debug e avviate l'interprete Python:

   1 >>> s = extending.hello('Mark')
   2 >>> s.greet()
   3 'Hello from Mark'
   4 >>> s.invite()
   5 'Hello from Mark! Please come soon!'

Ora entrate nella directory ~/libboost-doc/examples/libs/python/example e eseguite bjam:

...found 24 targets...
...updating 9 targets...
MkDir1 bin
MkDir1 bin/gcc-4.3
MkDir1 bin/gcc-4.3/debug
gcc.compile.c++ bin/gcc-4.3/debug/getting_started1.o
gcc.link.dll bin/gcc-4.3/debug/getting_started1.so
gcc.compile.c++ bin/gcc-4.3/debug/getting_started2.o
gcc.link.dll bin/gcc-4.3/debug/getting_started2.so
gcc.compile.c++ bin/gcc-4.3/debug/std_pair.o
gcc.link.dll bin/gcc-4.3/debug/std_pair_ext.so
...updated 9 targets...

Adesso entrate nella directory bin/gcc-4.3/debug e avviate l'interprete Python:

   1 >>> import getting_started1 as gs1
   2 >>> gs1.greet()
   3 'hello, w orld'
   4 >>> gs1.square(10)
   5 100
   6 >>> import getting_started2 as gs2
   7 >>> gs2.hello('Mark').greet()
   8 'Hello from Mark'
   9 >>> gs2.hello('Mark').invite()
  10 'Hello from Mark! Please come soon!'
  11 >>> import std_pair_ext as spe
  12 >>> spe.foo()
  13 (3, 5)

Se tutti questi passaggi sono stati fatti bene e non avete problemi, allora potete usare Boost.Python per creare i moduli Python in C++! Se notate problemi, chiedete nel forum , nella mailing list o nel canale IRC #boost su irc.freenode.net.

Collegamenti Esterni

Ringraziamenti

Per questo howto ringrazio di cuore l'autore di questo post.


CategoryDocumentazione

Librerie/EstenderePython/Boost/InstallareBoostSuDebian (last edited 2008-08-07 13:26:56 by Markon)