介子和boost_python的链接错误
Linking error with meson and boost_python
我正试图使用boost向python公开一个c++类,但我无法找出我遇到的链接错误。
我的演示程序如下:
#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'
我在网上看到了一些关于链接顺序的建议,我尝试过更改依赖数组的顺序,但没有成功。
从python3.8开始,python3.pc文件不提供根据嵌入式python解释器链接程序所需的库标志。相反,包需要使用python3嵌入,因此应该使用
python_dep = dependency('python3-embed')
还有两件事:
- 您正在构建python"模块";(因为您将在python中使用公开的方法greet((,对吧?(,因此您应该使用shared_library((作为目标(而不是executable(((
- 根据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'
>>>
看起来你还需要与python链接,因为出于某种原因,它没有作为boost的依赖项被拉入。python将dependency('python3')
添加到依赖项列表应该可以解决这个问题。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误