Python/C++扩展。在导入时,我得到一个未定义的符号

2024-05-23 13:01:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的设置.py以及用户方法.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

我不知道如何或在哪里这样一个符号将被定义或为什么它会被创建。有人知道这个错误是从哪里来的吗?提前谢谢。你知道吗

编辑: 这是你的名字设置.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],
      )

这是我的用户方法.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);

}

Tags: 文件pyincludelinuxlibsetupargsstatic
1条回答
网友
1楼 · 发布于 2024-05-23 13:01:12

根据上面的@Holt,这个错误是由于没有导入错误中包含类型定义的库引起的。你知道吗

我必须在我的链接步骤中添加到库的路径,我将其添加到扩展函数调用中的'extra\u link\u args'参数中设置.py. 你知道吗

相关问题 更多 >