i am trying to use boost to expose a c++ class to python, but i cannot figure out the linking error i am getting.
My demo program looks like this:
#include <boost/python.hpp>
#include <Python.h>
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
and my meson file looks like this:
project('test', 'cpp', version : '0.0.1',default_options : ['c_args= -g'])
add_project_arguments(language : 'cpp')
cxx = meson.get_compiler('cpp')
python_dep = dependency('python3')
boost_dep = dependency('boost',modules: ['python3'],)
src = ['./test.cpp']
executable('test', src,
dependencies : [ python_dep,boost_dep])
When i run it i get a bunch of undefined references to python methods:
/usr/include/boost/python/object_core.hpp:400: undefined reference to `_Py_NoneStruct'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_ValueError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_AsLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceFloorDivide'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyBool_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_GetAttr'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_GetItemString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_Call'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_And'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_IndexError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyEval_GetGlobals'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyTuple_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceSubtract'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_GetAttrString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyModule_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyMem_Free'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyType_IsSubtype'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyStaticMethod_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_WarnEx'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_InternFromString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyTuple_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_SetAttr'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_IsInstance'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_NoMemory'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_AttributeError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Add'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_FromFormat'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_Append'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyType_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_ReferenceError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyArg_ParseTupleAndKeywords'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyStaticMethod_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceMultiply'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyTuple_Size'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_SetObject'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `_PyObject_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Keys'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyType_GenericAlloc'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_Format'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_CallFunction'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_Reverse'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyCFunction_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_OverflowError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyMem_Malloc'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_ExceptionMatches'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyComplex_ImagAsDouble'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyImport_ImportModule'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyProperty_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_FromString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceOr'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyBytes_Size'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyType_GetFlags'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_DelItem'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_FloorDivide'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_Clear'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyRun_StringFlags'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_GetItem'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyWeakref_NewRef'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Multiply'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `_Py_NewReference'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Lshift'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_SetString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Update'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PySlice_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyRun_FileExFlags'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_SetItem'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_AsUTF8'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_SetAttrString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_FromEncodedObject'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_AsUnsignedLongLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_RuntimeError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Xor'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyBytes_AsString'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `_Py_NotImplementedStruct'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Rshift'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyBaseObject_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_StopIteration'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_GetItem'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyExc_TypeError'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Values'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `_Py_fopen'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyType_Ready'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_Size'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyMethod_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyMethod_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_ClearWeakRefs'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_AsSsize_t'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_AsLongLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyEval_CallFunction'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Remainder'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_FromLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_NewException'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_CallMethod'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Or'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_IsTrue'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_Sort'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyModule_Create2'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_AsWideChar'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_AsUnsignedLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceRemainder'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyErr_Occurred'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Copy'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceAnd'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceXor'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `_PyType_Lookup'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Size'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceRshift'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyFloat_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyIter_Next'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyCallable_Check'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyComplex_RealAsDouble'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyLong_FromUnsignedLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyList_Insert'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyObject_RichCompare'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Items'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyCMethod_New'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyTuple_GetItem'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_Subtract'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyComplex_Type'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_AsUTF8String'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyUnicode_FromStringAndSize'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyDict_Clear'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceLshift'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyBool_FromLong'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libboost_python39.so.1.74.0: undefined reference to `PyNumber_InPlaceAdd'
I seen some suggestions online about it being the order of the linking, i have tried changing the order of the dependency array without luck.
Starting from python3.8, the python3.pc file does not provide the library flags necessary to link programs against an embedded python interpreter. Instead, packages need to use python3-embed, so you should use
python_dep = dependency('python3-embed')
and two more things:
lib = shared_library('hello_ext',
src,
dependencies : [ python_dep, boost_dep],
name_prefix : '')
All steps, with complications with importing module from another path (with e.g. import importlib.util):
% meson build
% ninja -C build
% cd build
$ python3
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello_ext
>>> hello_ext.greet()
'hello, world'
>>>