python - 从 C Windowsx64 调用 Cython 函数

标签 python c gcc cython public

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

我正在尝试调用 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/

相关文章:

python - 从字典列表创建字典子集

python - 如何查看一段时间内收到的所有邮件?

c - 覆盖 gcov 函数以获取已执行的代码行

android - 链接器剥离未使用的类

python - 如何获取列表/元组的每个元素,其中列表作为参数传递给Python中的函数?

python - 如何将多维列变成单值向量以用于sklearn pandas中的训练数据

c - 如何使用 getch() ncurses 获取 Ctrl、Shift 或 Alt?

c - 为什么数组变量等于它的地址?

C - 从 ‘void*’ 到 ‘void (*)()’ 的无效转换

c - mingw32 显示对所有 gtk 函数的 undefined reference