【问题标题】:Calling a Cython Function form C Windowsx64从 C Windowsx64 调用 Cython 函数
【发布时间】:2014-07-28 07:25:22
【问题描述】:

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

我正在尝试从 C 中调用 Cython 函数。 我有以下代码:

print.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 (GCC) 4.7.0 20111219

Cython 版本 0.20.1

Windows 7 企业版 x64

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

最好的问候

【问题讨论】:

    标签: python c gcc cython public


    【解决方案1】:

    如果我是你,我会从 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

    【讨论】:

    • 感谢您的回答。但这不适合我。我希望你 cython 作为界面。
    猜你喜欢
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多