7

I have code written in C++:

#include <boost/python.hpp>

char const* greet()
{
  return "Yay!";
}

BOOST_PYTHON_MODULE(libtest)
{
  using namespace boost::python;
  def("greet", greet);
} 

Now i want to import this dynamic library to python by:

import libtest

But I get:

ImportError: /usr/lib/libboost_python.so.1.54.0: undefined symbol: PyClass_Type

What should I do? My OS is Arch Linux.

2 Answers 2

7

Ok, I have found solution for this problem. The simplest options is to compile by:

g++ testing.cpp -I/usr/include/python3.3m -I/usr/include/boost -lboost_python3 -lpython3.3m -o testing.so -shared -fPIC

Previously I used -lboost_python instead of -lboost_python3 ... But this solution is not cross platform so we can achieve this by cmake:

cmake_minimum_required(VERSION 2.6)


find_package(Boost 1.54 EXACT REQUIRED COMPONENTS python3)

INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS} "/usr/include/python3.3m/" )
find_package(PythonLibs)
ADD_LIBRARY(testing SHARED testing.cpp)
TARGET_LINK_LIBRARIES(testing ${Boost_LIBRARIES} ${PythonLibs_LIBRARIES})

Of course "/usr/include/python3.3m" won't be path to pythons include directory in all linux distros.

3
2

Use the same version of Python when building both Boost.Python and the libtest module, as well as when importing libtest.

PyClass_Type is is part of the Python 2 C API and not part of the Python 3 C API. Hence, the Boost.Python library was likely built against Python 2. However, it is being loaded by a Python 3 interpreter, where the PyClass_Type is not available.

2
  • So I tried to import libtest in Python 2 and I got: ImportError: ./libtest.so: undefined symbol: _ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
    – Yuras
    Commented Nov 9, 2013 at 21:23
  • @Yuras: That symbol is because libtest is compiled against Python 3, but being loaded by Python 2. Just wanted to stress that one has to use the same version of Python when building both Boost.Python and the libtest module, as well as when importing libtest. Commented Nov 10, 2013 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.