2011-08-25 19 views
7

Así que estoy tratando de interactuar con python 3.2 y C++ usando boost python, y he encontrado muchos problemas. Finalmente lo compilé usando las bibliotecas 2.7 y funciona, pero parece que no puedo hacerlo funcionar con Python 3.2.Hola mundo con boost python y python 3.2

Aquí está el código C++

#include <iostream> 

using namespace std; 

void say_hello(const char* name) { 
    cout << "Hello " << name << "!\n"; 
} 

int main(){return 0;} 

#include <boost/python/module.hpp> 
#include <boost/python/def.hpp> 
using namespace boost::python; 

BOOST_PYTHON_MODULE(hello) 
{ 
    def("say_hello", say_hello); 
} 

Si puedo compilar usando los 2,7 bibliotecas funciona muy bien, pero cuando se utiliza las bibliotecas 3.2 consigo toneladas de referencias indefinidas de libboost_python.so

lo contrario escribí un poco de Python para hacer que funcione:

from distutils.core import setup 
from distutils.extension import Extension 

setup(name="PackageName", 
    ext_modules=[ 
     Extension("hello", ["testBoost.cpp"], 
     libraries = ["boost_python"]) 
    ]) 

y esto creará un modo usando Python 3.2 o 2.7 build, pero cuando abro la interpr Python 3 eter e intento importar el valor para que me dé el error indefinido símbolo PyClass_Type de libboost_python.so nuevamente. ¿Algunas ideas? ¿Es boost python compatible con Python 3.x?

Si la información es útil, aquí está mi intento de compilación usando 3.2:

$ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python -lpython3.2mu 
    /tmp/ccdmU1Yu.o: In function `PyInit_hello': 
    testBoost.cpp:(.text+0xc2): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_Size' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyFile_FromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromStringAndSize' 
    /usr/local/lib/libboost_python.so: undefined reference to `Py_InitModule4_64' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_FromFormat' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyNumber_Divide' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyNumber_InPlaceDivide' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_AsLong' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_InternFromString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyClass_Type' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyString_AsString' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyInt_FromLong' 
    /usr/local/lib/libboost_python.so: undefined reference to `PyFile_AsFile' 
    collect2: ld returned 1 exit status 

Y el error del intérprete de Python 3 es

File "<stdin>", line 1, in <module> 
ImportError: /usr/local/lib/libboost_python.so.1.47.0: undefined symbol: PyClass_Type 

Gracias por cualquier ayuda!

+1

Es posible que desee considerar buscar en SWIG en lugar de Boost.Python, si puede.Requiere mucho menos código repetitivo y he conseguido que funcione con Python3 con bastante facilidad. – Sean

+2

@Sean No estoy seguro de qué código estándar está usted hablando; mi boost/python funciona maravillosamente con solo 5 líneas de código extra. – steventrouble

Respuesta

5

Lo anterior C++ código se compila en un módulo con

$ g++ testBoost.cpp -I/usr/include/python3.2 -I/usr/local/include/boost/python -lboost_python3 -lpython3.2mu -o hello.so -shared 

Este comando de compilación añade -lboost_python3, y -shared, y también la convención de nomenclatura para los módulos de extensión de Python. También debe instalar el paquete python3-dev y configurar/crear/instalar boost con python3, si no lo ha hecho aún.

en Python 3, que a continuación, puede hacer lo siguiente:

$ python3 
Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) 
[GCC 4.5.2] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import hello 
>>> hello.say_hello('bill') 
Hello bill! 
>>> 

usted debe estar fuera de las carreras en ese punto.

+1

Intenté usar -lboost_python3 pero todavía recibo el mismo error, y ya he cambiado mi archivo jam para apuntar a python 3 – Dwight

+1

Bien, así que compilé con sus instrucciones, y me dijo que necesitaba recompilar con -fpic, lo que funcionó , y terminé con el .so. Ahora, sin embargo, cuando trato de importar en python, aparece un error que dice: ImportError: /usr/local/lib/libboost_python3.so.1.47.0: undefined symbol: PyClass_Type – Dwight

8

Tuve exactamente el mismo problema, con Ubuntu 12.04. Instalé la versión 1.48 de la biblioteca y tuve que vincular con libboost_python-py32.so en lugar de libboost_python.so Después de esto, los errores del enlazador habían desaparecido.

1

Aunque esta discusión de edad, sólo para el registro: Modificar proyecto config.jam para cambiar la versión de Python para la configuración de su

# Python configuration 
using python : 3.4 : /usr ; 

luego construir impulso:

./b2 clean 
./b2 --with-python link=static cxxflags="-std=c++11 -fPIC" variant=release stage 
./b2 --with-python link=static cxxflags="-std=c++11 -fPIC" variant=release install 

El comando más tarde requiere privilegios de superusuario. A continuación, pasar a la carpeta que contiene el código de C++ para la extensión:

g++ -std=c++11 hellopy.cpp -I/usr/include/python3.4 -I/usr/local/include/boost/python -lboost_python3 -o hello.so -shared -fPIC 

A continuación, puede importar hola en su entorno de pitón.