【发布时间】:2019-11-12 10:47:30
【问题描述】:
我的 setup.py 和 UserMethods.cpp 文件如下。
我的问题是这样的:我正在尝试使用 distutils 创建和安装 python 包,但遇到了一些问题。
当我运行python3 setup.py install --user 时没有问题。它编译并创建一个build/ 目录,其中包含一个名为lib.linux-x86_64-3.6 的文件。当我检查我的.local/lib/python3.6/site-pacages 目录时,有一个名为UserMethods.cpython-36m-x86_64-linux-gnu.so 的文件。
当我尝试导入包时出现问题:
$ python3
>>> import UserMethods
返回以下错误:
ImportError: ~/.local/lib/python3.6/site-packages/UserMethods.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN12NA62Analysis4Core18AnalyzerIdentifierD1Ev
我不知道如何或在何处定义这样的符号,也不知道为什么要创建它。有没有人知道这个错误来自哪里?提前致谢。
编辑: 这是 setup.py 文件:
from distutils.core import setup, Extension
UM_module = Extension('UserMethods', sources=['UserMethodsModule.cpp'], language='C++',
include_dirs=[ ...many... ],
extra_compile_args=['-std=c++11'],
libraries=['stdc++'],)
setup(name='UserMethods',
version='1.0',
ext_modules=[UM_module],
)
这是我的 UserMethods.cpp 文件:
#include <Python.h>
#define PY_SSIZE_T_CLEAN
#include "UserMethods.hh"
/* OUR FUNCTIONS GO HERE */
static PyObject* UM_test(PyObject *self, PyObject *args){
const char *command;
int sts;
if ( !PyArg_ParseTuple(args, "s", &command) ){
return NULL;
}
sts = system(command);
return PyLong_FromLong(sts);
}
static PyMethodDef UserMethods[] = {
{"system", UM_test, METH_VARARGS, "execute shell command."},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef UserMethodsModule = {
PyModuleDef_HEAD_INIT,
"UserMethods",
NULL,
-1,
UserMethods
};
PyMODINIT_FUNC PyInit_UserMethods(void){
return PyModule_Create(&UserMethodsModule);
}
【问题讨论】:
-
UserMethods.cpp和setup.py的内容是什么?这看起来像是您用于编译库的编译器与用于编译 python 的编译器之间的不兼容。 -
符号是
NA62Analysis::Core::AnalyzerIdentifier::~AnalyzerIdentifier()的修改版(来源:c++filt) -
@Holt 我添加了请求的代码。我尝试注释掉
#include UserMethods.hh行,一切都编译得很好。我认为这与编译器如何导入模块有关? -
我写“include_dirs”列表是否有特定的顺序?我有 ~20 个包含目录
-
@DaneCross 显然您使用的是
NA62Analysis,因此您可能需要将所需的动态库添加到您的 PATH 中。