简体   繁体   English

与介子和 boost_python 链接错误

[英]Linking error with meson and boost_python

i am trying to use boost to expose a c++ class to python, but i cannot figure out the linking error i am getting.我正在尝试使用 boost 将 c++ class 暴露给 python,但我无法弄清楚我得到的链接错误。

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:当我运行它时,我得到一堆对 python 方法的未定义引用:

/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.我在网上看到了一些关于它是链接顺序的建议,我试过改变依赖数组的顺序但没有运气。

That looks like you also need to link with python, because for some reason it's not being pulled in as a dependency of boost python adding dependency('python3') to your dependency list should solve that.看起来您还需要与 python 链接,因为由于某种原因,它没有被作为 boost python 的dependency('python3')项添加到您的依赖项列表中,应该可以解决这个问题。

Starting from python3.8, the python3.pc file does not provide the library flags necessary to link programs against an embedded python interpreter.从 python3.8 开始,python3.pc 文件不提供将程序链接到嵌入式 python 解释器所需的库标志。 Instead, packages need to use python3-embed, so you should use相反,包需要使用 python3-embed,所以你应该使用

python_dep = dependency('python3-embed')

and two more things:还有两件事:

  • you are building python "module" (since you will use your exposed method greet() in python, right?), so you should use shared_library() as target (not executable())您正在构建 python“模块”(因为您将在 python 中使用您公开的方法 greet(),对吧?),所以您应该使用shared_library()作为目标(不是可执行文件())
  • according to the way how python is looking for modules - the library should be named exactly as your named it BOOST_PYTHON_MODULE (in the simplest case, alternatives you can see here ), thus the name suffix is overridden (default is 'lib')根据 python 查找模块的方式 - 库的名称应与您命名的 BOOST_PYTHON_MODULE 完全相同(在最简单的情况下,您可以在此处看到替代方案),因此名称后缀被覆盖(默认为“lib”)
 lib = shared_library('hello_ext',
            src,
            dependencies : [ python_dep, boost_dep],
            name_prefix : '')

All steps, with complications with importing module from another path (with eg import importlib.util):所有步骤,以及从另一个路径导入模块的复杂性(例如 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'
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM