Adam
Adam

Reputation: 4172

Custom Python extension - Import error: undefined symbol

Learning how to write a C extension in Python. Running into the following error when executing ptest.py

Traceback (most recent call last):
  File "ptest.py", line 1, in <module>
    import euler_py as eul
ImportError: /usr/local/lib/python3.6/site-packages/euler_py.cpython-
36m-x86_64-linux-gnu.so: undefined symbol: problem_one

I'm assuming this is some type of linking issue.

setup.py

sources = glob.glob('ext/*.c')

euler = Extension(
    'euler_py',
    include_dirs=['src'],
    sources=sources,
    extra_compile_args=['-std=c99']
)

setup(
    name='euler_py',
    version='0.1',
    description='Project Euler Solutions',
    ext_modules=[euler]
)

ptest.py

import euler_py as eul

print(eul.problem_one(10))

The underlying functions are in /src and I wrote test.c to test purely in C. My extension is in /ext/euler_py.c shown below

ext/euler_py.c

#include <Python.h>
#include "../src/euler.h"

static char module_docstring[] = "Provides interface to Project Euler problems";

/*
 * Function implementations
 */


static PyObject* euler_py_problem_one(PyObject *self, PyObject *args)
{
    int max, result;

    if (!PyArg_ParseTuple(args, "i", &max))
        return NULL;

    result = problem_one(max);

    return Py_BuildValue("i", result);
}
// END function implementations

// Wire in functions to module
static PyMethodDef module_methods[] = {
        {"problem_one", euler_py_problem_one, METH_VARARGS, "Solution to problem 1"},
        {NULL, NULL, 0, NULL}
};


// Module definition
static struct PyModuleDef euler_py_module = {
        PyModuleDef_HEAD_INIT,
        "euler_py",
        module_docstring,
        -1,
        module_methods
};

// Module initialization function
PyMODINIT_FUNC PyInit_euler_py(void)
{
    return PyModule_Create(&euler_py_module);
}

Repo is here. I've played around with library_dirs & include_dirs on the Extension() initiation and no luck. Python version 3.6. Need a second look.

EDIT

Repo linked to has changed since original ask. Added linking for other 3 functions in the same manor as previous.

Upvotes: 2

Views: 3067

Answers (1)

georgexsh
georgexsh

Reputation: 16624

You forget to include all source files:

sources = glob.glob('ext/*.c') + glob.glob('src/*.c')

you could see c extension build detail with setup.py build -fv:

$ python setup.py build -fv
...
clang -bundle -undefined dynamic_lookup build/temp.macosx-10.13-x86_64-3.6/ext/euler_py.o build/temp.macosx-10.13-x86_64-3.6/src/euler.o build/temp.macosx-10.13-x86_64-3.6/src/helpers.o -o build/lib.macosx-10.13-x86_64-3.6/euler_py.cpython-36m-darwin.so

now helpers.o and euler.o properly linked.

Upvotes: 4

Related Questions