gpt4 book ai didi

python - 从 C Windowsx64 调用 Cython 函数

转载 作者:行者123 更新时间:2023-11-30 17:30:45 25 4
gpt4 key购买 nike

经过长时间的搜索没有任何结果,我需要一些帮助。

我正在尝试调用 C 形式的 Cython 函数。我有以下代码:

打印.pyx

cdef public int grail(int i, int a): # public function declaration
return (i+a)

modul.c

#include <Python.h>
#include "print.h"
#include <stdio.h>
void main(void) {
int a;

Py_Initialize();
initprint();
a = grail(2,3);
printf("%i\n", a);
Py_Finalize();
}

我尝试了两种方法来编译和链接它,但链接总是失败。

首先使用命令行:

cython print.pyx
gcc -LD:\Anaconda\libs -ID:\Anaconda\include -o print.exe print.c modul.c -lpython27 -g

第二个是setup.py

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("print", ["print.pyx", "modul.c"])]
)

然后在命令行上:

python setup.py build_ext --inplace

在这两种情况下我都遇到了相同的错误:

build\temp.win32-2.7\Release\modul.o:modul.c:(.text+0x2a): undefined reference t
o `__imp__grail'

以下是有关我的系统的一些附加信息:

Python 2.7.7::Anaconda 2.0.1(32 位)

海湾合作委员会(GCC)4.7.0 20111219

Cython 版本 0.20.1

Windows 7 企业版 x64

我希望有人知道我如何解决这个问题,但已经非常感谢您的帮助。

致以诚挚的问候

最佳答案

如果我是你,我会从 CFFI 开始,而不是手动编写:

http://eli.thegreenplace.net/2013/03/09/python-ffi-with-ctypes-and-cffi

不过,我不确定它在 Windows 上的运行情况如何(对我来说,它在 Linux 上运行得很好)。它可能会起作用,因为 CFFI 项目声称这是关于 Windows 的:

http://cffi.readthedocs.org/en/release-0.8/#windows-64

关于python - 从 C Windowsx64 调用 Cython 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990320/

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