I'm trying to create a helloWorld module for Python3 in C++ using boost::python library.
Here is a CmakeList.txt
:
set(Python_ADDITIONAL_VERSIONS 3.4)
find_package( PythonLibs 3.4 REQUIRED )
include_directories( ${PYTHON_INCLUDE_DIRS} )
find_package( Boost 1.56.0 EXACT COMPONENTS python3 REQUIRED )
include_directories( ${Boost_INCLUDE_DIR} )
# Define the wrapper library that wraps our library
add_library( hello SHARED main.cpp )
target_link_libraries( hello ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES} )
# don't prepend wrapper library name with lib
set_target_properties( hello PROPERTIES PREFIX "" OUTPUT_NAME hello)
main.cpp
#include <boost/python.hpp>
char const* greet( )
{
return "Hello world";
}
BOOST_PYTHON_MODULE(mymodule)
{
using namespace boost::python;
def( "greet", greet );
}
I installed boost libraries from source like described here, but it does not allow me to use boost-python3 library (have an error in Cmake). For this purpose I used
./bootstrap.sh --with-python-version=3.4 --prefix=/usr/local
instead of
./bootstrap.sh --prefix=/usr/local
to explicitly specify version of python;
As an output we get a shared library hello.so
. All seems to be ok. But...
When I try to import the library to python script sript.py
with content:
import hello
in terminal using command ...$ python3 script.py
I receive an error
Traceback (most recent call last):
File "script.py", line 1, in <module>
import hello
ImportError: /usr/local/lib/libboost_python3.so.1.56.0: undefined symbol: PyClass_Type
The question is: How to make boost library compatible with python3? There are no problems with python2
. But I need python3
.
I also saw the page when the same error happens but it didn't help me.
My software:
- boost version 1.56.0
- pyhton 3.4
- cmake version 2.8.12.2
- gcc 4.8.2
- OS: Ubuntu 14.04 LTS, 64 bit