gpt4 book ai didi

python - Cython: undefined symbol

转载 作者:行者123 更新时间:2023-12-04 21:31:10 26 4
gpt4 key购买 nike

我编写了一个最小的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com