簡體   English   中英

與介子和 boost_python 鏈接錯誤

[英]Linking error with meson and boost_python

我正在嘗試使用 boost 將 c++ class 暴露給 python,但我無法弄清楚我得到的鏈接錯誤。

我的演示程序如下所示:

#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);
}

我的介子文件如下所示:

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])

當我運行它時,我得到一堆對 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'

我在網上看到了一些關於它是鏈接順序的建議,我試過改變依賴數組的順序但沒有運氣。

看起來您還需要與 python 鏈接,因為由於某種原因,它沒有被作為 boost python 的dependency('python3')項添加到您的依賴項列表中,應該可以解決這個問題。

從 python3.8 開始,python3.pc 文件不提供將程序鏈接到嵌入式 python 解釋器所需的庫標志。 相反,包需要使用 python3-embed,所以你應該使用

python_dep = dependency('python3-embed')

還有兩件事:

  • 您正在構建 python“模塊”(因為您將在 python 中使用您公開的方法 greet(),對吧?),所以您應該使用shared_library()作為目標(不是可執行文件())
  • 根據 python 查找模塊的方式 - 庫的名稱應與您命名的 BOOST_PYTHON_MODULE 完全相同(在最簡單的情況下,您可以在此處看到替代方案),因此名稱后綴被覆蓋(默認為“lib”)
 lib = shared_library('hello_ext',
            src,
            dependencies : [ python_dep, boost_dep],
            name_prefix : '')

所有步驟,以及從另一個路徑導入模塊的復雜性(例如 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