简体   繁体   English

自定义Python扩展 - 导入错误:未定义的符号

[英]Custom Python extension - Import error: undefined symbol

Learning how to write a C extension in Python. 学习如何在Python中编写C扩展。 Running into the following error when executing ptest.py 执行ptest.pyptest.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 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 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 底层函数在/src ,我编写了test.c以纯粹在C中进行测试。我的扩展名在/ext/euler_py.c中,如下所示

ext/euler_py.c 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. 我在Extension()启动时玩过library_dirsinclude_dirs而没有运气。 Python version 3.6. Python版本3.6。 Need a second look. 需要再看看。

EDIT 编辑

Repo linked to has changed since original ask. Repo链接自原始问题后已发生变化。 Added linking for other 3 functions in the same manor as previous. 添加了与之前相同的其他3个功能的链接。

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 : 您可以使用setup.py build -fv查看c扩展构建详细信息:

$ 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. 现在helpers.oeuler.o正确链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python 3.6中调用C ++扩展时,导入错误“未定义符号:_ZNK9FastNoise8GetNoiseEff” - Import error “undefined symbol: _ZNK9FastNoise8GetNoiseEff” when calling C++ extension in Python 3.6 Python / C ++扩展。 导入时,出现未定义的符号错误 - Python/C++ extension. On import, I get an undefined symbol error Python/C++ 扩展,链接库时出现未定义符号错误 - Python/C++ Extension, Undefined symbol error when linking a library 在python中导入CV2给出未定义的符号错误 - Import CV2 in python give undefined symbol error DSX Python导入错误:未定义符号:PyUnicodeUCS2_AsUTF8String - DSX Python import error : undefined symbol: PyUnicodeUCS2_AsUTF8String Python3 导入错误:未定义符号:aes_hw_encrypt - Python3 Import Error: undefined symbol: aes_hw_encrypt python 上的 SWIG 错误导入“未定义符号:mysql_query” - SWIG error on python import 'undefined symbol: mysql_query' SWIG Python 未定义符号错误 - SWIG Python undefined symbol error C ++ Python模块导入错误:“undefined symbol:Py_InitModule3”(Py_InitModule()) - C++ Python module import error: “undefined symbol: Py_InitModule3” ( Py_InitModule () ) Python opencv 导入错误(未定义符号)当我运行脚本时,在我安装 ros k.netic 之后 - Python opencv import error(undefined symbol) when i run the script , after i install ros kinetic
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM