Hello world with boost python and python 3.2

So, I am trying to connect python 3.2 and C ++ using boost python and have encountered a lot of problems. I finally got it for compilation using libraries 2.7 and it works, but I can't get it to work with python 3.2.

Here is the C ++ code

#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); } 

If I compile it using libraries 2.7, it works fine, but when I use libraries 3.2, I get tons of undefined links from libboost_python.so

Otherwise, I wrote a little python to make it work:

 from distutils.core import setup from distutils.extension import Extension setup(name="PackageName", ext_modules=[ Extension("hello", ["testBoost.cpp"], libraries = ["boost_python"]) ]) 

and it will create like this using python 3.2 or 2.7 build, but when I open the python 3 interpreter and try to import it, it will again give me the undefined error PyClass_Type symbol from libboost_python.so. Any ideas? Is boost python compatible with python 3.x?

If the information is useful, here is my attempt to compile using 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 

And error from python 3 interpreter

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

Thanks for any help!

+7
source share
3 answers

I had the same issue with Ubuntu 12.04. I installed the library version 1.48 and contacted libboost_python-py32.so instead of libboost_python.so . After that, the linker errors disappeared.

+8
source

The above C ++ code compiles into a module with

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

This compilation command adds -lboost_python3 and -shared , as well as the naming convention for the python extension modules. You must also install the python3-dev package and configure / build / install boost with python3 if you have not already done so.

In python 3, I can do the following:

 $ 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! >>> 

At this point you must go to the race.

+5
source

Although this discussion is old, just for the record: Modify the config.jam project to change the python version to your setup

 # Python configuration using python : 3.4 : /usr ; 

Then create boost:

 ./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 

For a later command, superuser privileges are required. Then go to the folder containing the C ++ code for the extension:

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

Then you can import hello into your python environment.

+1
source

All Articles