1

I'm trying to write some simple c++ objects that I would like to expose in python. Following bit of instructions on Extending to python3 from boost python, I have the following -

#include <boost/python.hpp>

int someint() {
    return 0;
}

BOOST_PYTHON_MODULE(cpp_mod)
{
    using namespace boost::python;
    def("someint", someint);
}

adding as the python module, in setup.py.

from distutils.core import setup, Extension

example_module = Extension(
    'pyobj',
    sources=['pyobject.cpp'],
    language='C++', )

setup(
    name='pyobj',
    version='0.1.0',
    description='example module written in C++',
    ext_modules=[example_module], ) 

results in

>> import example
ImportError: /usr/local/lib/python3.8/dist-packages/example.cpython-38-x86_64-linux-gnu.so: undefined symbol: _ZTVN10__cxxabiv120__si_class_type_infoE

----

compling with g++, results in

g++ -I /usr/include/python3.8 pyobj.cpp

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `PyInit_hello_ext':
test.cpp:(.text+0x9d): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `_Py_DECREF':
test.cpp:(.text._Py_DECREF[_ZN5boost6python3api11object_baseD5Ev]+0x3e): undefined reference to `_Py_Dealloc'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::type_info::name() const':
test.cpp:(.text._ZNK5boost6python9type_info4nameEv[_ZNK5boost6python9type_info4nameEv]+0x23): undefined reference to `boost::python::detail::gcc_demangle(char const*)'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::to_python_value<int const&>::operator()(int const&) const':
test.cpp:(.text._ZNK5boost6python15to_python_valueIRKiEclES3_[_ZNK5boost6python15to_python_valueIRKiEclES3_]+0x20): undefined reference to `PyLong_FromLong'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::to_python_value<int const&>::get_pytype() const':
test.cpp:(.text._ZNK5boost6python15to_python_valueIRKiE10get_pytypeEv[_ZNK5boost6python15to_python_valueIRKiE10get_pytypeEv]+0xf): undefined reference to `PyLong_Type'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::api::object::object()':
test.cpp:(.text._ZN5boost6python3api6objectC2Ev[_ZN5boost6python3api6objectC5Ev]+0x18): undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `void boost::python::def<int (*)()>(char const*, int (*)())':
test.cpp:(.text._ZN5boost6python3defIPFivEEEvPKcT_[_ZN5boost6python3defIPFivEEEvPKcT_]+0x54): undefined reference to `boost::python::detail::scope_setattr_doc(char const*, boost::python::api::object const&, char const*)'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::api::object boost::python::detail::make_function_aux<int (*)(), boost::python::default_call_policies, boost::mpl::vector1<int> >(int (*)(), boost::python::default_call_policies const&, boost::mpl::vector1<int> const&)':
test.cpp:(.text._ZN5boost6python6detail17make_function_auxIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEENS0_3api6objectET_RKT0_RKT1_[_ZN5boost6python6detail17make_function_auxIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEENS0_3api6objectET_RKT0_RKT1_]+0x61): undefined reference to `boost::python::objects::function_object(boost::python::objects::py_function const&)'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::objects::py_function_impl_base::py_function_impl_base()':
test.cpp:(.text._ZN5boost6python7objects21py_function_impl_baseC2Ev[_ZN5boost6python7objects21py_function_impl_baseC5Ev]+0xf): undefined reference to `vtable for boost::python::objects::py_function_impl_base'
/usr/bin/ld: /tmp/ccBDVo8E.o:(.data.rel.ro._ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEEE[_ZTVN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEEE]+0x30): undefined reference to `boost::python::objects::py_function_impl_base::max_arity() const'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::objects::caller_py_function_impl<boost::python::detail::caller<int (*)(), boost::python::default_call_policies, boost::mpl::vector1<int> > >::~caller_py_function_impl()':
test.cpp:(.text._ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEED2Ev[_ZN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEED5Ev]+0x26): undefined reference to `boost::python::objects::py_function_impl_base::~py_function_impl_base()'
/usr/bin/ld: /tmp/ccBDVo8E.o:(.data.rel.ro._ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEEE[_ZTIN5boost6python7objects23caller_py_function_implINS0_6detail6callerIPFivENS0_21default_call_policiesENS_3mpl7vector1IiEEEEEE]+0x10): undefined reference to `typeinfo for boost::python::objects::py_function_impl_base'
/usr/bin/ld: /tmp/ccBDVo8E.o: in function `boost::python::converter::expected_pytype_for_arg<int>::get_pytype()':
test.cpp:(.text._ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv]+0x1f): undefined reference to `boost::python::converter::registry::query(boost::python::type_info)'
/usr/bin/ld: test.cpp:(.text._ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv[_ZN5boost6python9converter23expected_pytype_for_argIiE10get_pytypeEv]+0x36): undefined reference to `boost::python::converter::registration::expected_from_python_type() const'
collect2: error: ld returned 1 exit status

I don't understand the reason behind neither the first problem nor the latter.

PatXio
  • 129
  • 2
  • 16
  • Please show the *precise* G++ command line. – nanofarad Feb 04 '21 at 15:34
  • yes, my apologies! added update! – PatXio Feb 04 '21 at 16:57
  • You're going to need to add a linker flag of some kind to instruct g++ to link the binary to the actual Python binaries. I'm not sure of the exact details but [this](https://docs.python.org/3/extending/embedding.html) recommends running a command like `pythonX.Y-config --ldflags` to get the correct linker flags (and gives example flags if you want to try to adapt them but cannot run that command). – nanofarad Feb 04 '21 at 17:04
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ulrich Eckhardt Feb 04 '21 at 18:14
  • 1
    @nanofarad it works with `-lpython3.8` – PatXio Feb 04 '21 at 18:34
  • @PatXio Awesome, glad to hear it. If you'd like to write a self-answer and accept it feel free to do so. Feel free to paraphrase my comment to give deeper context. – nanofarad Feb 04 '21 at 18:36
  • so that works for stuff under `Python.h` but I'm not sure what flags to use when using `boost/python.hpp`. do you have any idea @nanofarad? – PatXio Feb 05 '21 at 13:15
  • @PatXio I don't unfortunately. – nanofarad Feb 05 '21 at 15:33
  • 1
    @nanofarad I figured it out. one needs the `-libboost_python` flag. You can find it using `dpkg -L libboost_python*` as it may vary depending on different python binaries. – PatXio Feb 05 '21 at 16:09

0 Answers0