简体   繁体   中英

Python/C++ extension. On import, I get an undefined symbol error

My setup.py and UserMethods.cpp file are below.

My issue is this: I am trying to create and install a python package using distutils and I am running into some issues.

When I run python3 setup.py install --user there is no issue. It compiles and creates a build/ directory with a file named lib.linux-x86_64-3.6 . When I check my .local/lib/python3.6/site-pacages directory, there is a file named UserMethods.cpython-36m-x86_64-linux-gnu.so .

The issue comes when I try to import the package:

$ python3
>>> import UserMethods

which returns the following error:

ImportError: ~/.local/lib/python3.6/site-packages/UserMethods.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN12NA62Analysis4Core18AnalyzerIdentifierD1Ev

I do not know how or where such a symbol would be defined or why it would be created. Does anyone have insight as to where this error is coming from? Thanks in advance.

EDIT: here is the setup.py file:

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

and here is my UserMethods.cpp file:

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

}

as per @Holt above, this error was caused by not importing the library that contains the type definition in the error.

I had to add the path to the library in my linking step, which I added to the 'extra_link_args' argument in the Extension function call in Setup.py.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM