下面是我的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);
}
发布于 2019-07-03 10:12:40
根据上面的@Holt,这个错误是由于没有导入包含错误中类型定义的库而导致的。
我必须在我的链接步骤中添加到库的路径,我将其添加到Setup.py中扩展函数调用的'extra_link_args‘参数中。
https://stackoverflow.com/questions/56841420
复制