python - Cython: undefined symbol

标签 python cython

我编写了一个最小的 C 函数(带有随附的头文件),目的是创建一个 Cython 包装器,该包装器可以在其他地方的 Python 文件中使用 C 代码。

我的文件是这样的:

C 文件:

/* engine.c */

#include <stdio.h>
#include "customlib.h"

int engine(int value, int iterations) {

    /* declare iteration index */
    int idx;

    for (idx = 0; idx < iterations; idx = idx + 1) {
        value = value * 3;
    }

    /* return value that should be accessible by the Python module */
    return value;
}

C头文件:
/* customlib.h */

#ifndef CUSTOM_HEADER_H
#define CUSTOM_HEADER_H

/* engine function */
int engine(int value, int iterations);

#endif

包装 C 代码的 Cython 模块:
# wrapper.pyx

cdef extern from "customlib.h":
    int engine(int value, int iterations)

def call_c_code(value, iterations):
    output_value = engine(value, iterations)
    print(output_value)

通过 Cython 包装器调用 C 代码的 Python 模块:
# caller.py

import wrapper

wrapper.call_c_code(1, 2) /* values not important; just an example */

从 *.pyx 文件生成 *.so 的设置代码:
# setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

# definitions
sourcefiles = ['engine.c']
extensions = [Extension('wrapper', sourcefiles)]

# setup function
setup(name='wrapper', ext_modules=cythonize('wrapper.pyx'))

问题:共享对象 (*.so) 似乎编译没有任何问题。但是,即使只是导入 wrapper.py引发以下错误:
Traceback (most recent call last):
  File "caller.py", line 10, in <module>
    import wrapper
ImportError: /home/alex/Documents/Projects/Cython/wrapper.cpython-36m-x86_64-linux-gnu.so: undefined symbol: engine

最佳答案

在您的 setup.py你实际上没有做任何事情:

extensions = [Extension('wrapper', sourcefiles)]

本质上,这只是死代码。它分配给一个变量,然后从不使用它。无论如何,您不想制作 Extension来自您的engine.c . Extension用于定义 Python 模块。这里唯一的 Python 模块是从 wrapper.pyx 编译的。 .

相反,请尝试以下操作:
extensions = [Extension('wrapper', ['wrapper.pyx', 'engine.c'])]
setup(
    ...
    ext_modules=cythonize(extensions)
    ...
 )

这也将编译 engine.c并将其生成的目标文件链接到您的扩展模块。
cythonize辅助函数足够聪明,可以区分普通 .c来自 Cython 的来源,它需要通过 Cython 编译器。然后它将通过 C 编译器传递所有生成的 C 源代码并链接它们。

关于python - Cython: undefined symbol ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272170/

相关文章:

python - 是否有内置的 Python 函数在将函数映射到可迭代对象时返回第一个 True-ish 值?

python - C(P)ython 或 D 中的多平台 gui 应用程序

python - cythoning 类时出现空声明符错误?

python - 用 cython 包装 c++ 和 CUDA 代码

python - Cython 尝试编译两次,但失败了

cython - 如何在 Cython 中正确包含头文件(setup.py)?

python - 如何在 Ubuntu 18.04 中将 pip3 更新到最新版本?

Python Windows 身份验证用户名和密码不起作用

python - 使用python如何检查模块是否已加载?

python - 使用 Python,如何获取我的 Google protobuf 消息的二进制序列化?