我有一个Python程序,我想从C程序调用。我被告知有一种方法可以使用Cython从我的Python代码创建一个C库。
我是Cython的新手,我真的不明白它是如何运作的。到目前为止,我在我的Linux机器上安装了Cython,并将我的Python程序放在Cython-0.22.1文件夹中。
这是我到目前为止所尝试的内容。
module.py
文件重命名为module.pyx
文件。我修改了Cython文件夹中的setup.py
文件,看起来像这样:
from distutils.core import setup
from Cython.Build import cythonize
setup(name = 'First try', ext_modules = cythonize("module.pyx"),)
我在shell中运行了这个命令:cython module.pyx
。这在同一文件夹中创建了一个module.c
文件。
gcc module.c
但是,我得到error: Python.h no such file or directory.
所以我执行了locate Python.h
并选择了一个文件位置,所以我运行了这个:
gcc -I /root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/ module.c
gcc -fPIC -c -I /root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include module.c
,但这也没有解决我的参数问题。 所以我被困在一堆未解决的参数。以下是这些错误的代码:
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/memoryobject.h:6
8: error: declaration for parameter ‘PyMemoryViewObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/stringobject.h:4
9: error: declaration for parameter ‘PyStringObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/complexobject.h:
43: error: declaration for parameter ‘PyComplexObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/complexobject.h:
12: error: declaration for parameter ‘Py_complex’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/floatobject.h:17
: error: declaration for parameter ‘PyFloatObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/longobject.h:10:
error: parameter ‘PyLongObject’ has incomplete type
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/longobject.h:10:
error: declaration for parameter ‘PyLongObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/intobject.h:26:
error: declaration for parameter ‘PyIntObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/unicodeobject.h:
423: error: declaration for parameter ‘PyUnicodeObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/unicodeobject.h:
137: error: declaration for parameter ‘Py_UNICODE’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/unicodeobject.h:
128: error: declaration for parameter ‘Py_UCS4’ but no such parameter
/usr/include/wchar.h:824: error: declaration for parameter ‘wcsftime’ but no suc
h parameter
/usr/include/wchar.h:756: error: declaration for parameter ‘ungetwc’ but no such
parameter
/usr/include/wchar.h:749: error: declaration for parameter ‘fputws’ but no such
parameter
/usr/include/wchar.h:742: error: declaration for parameter ‘fgetws’ but no such
parameter
/usr/include/wchar.h:733: error: declaration for parameter ‘putwchar’ but no suc
h parameter
/usr/include/wchar.h:727: error: declaration for parameter ‘putwc’ but no such p
arameter
/usr/include/wchar.h:726: error: declaration for parameter ‘fputwc’ but no such
parameter
/usr/include/wchar.h:719: error: declaration for parameter ‘getwchar’ but no suc
h parameter
/usr/include/wchar.h:713: error: declaration for parameter ‘getwc’ but no such p
arameter
/usr/include/wchar.h:712: error: declaration for parameter ‘fgetwc’ but no such
parameter
/usr/include/wchar.h:510: error: declaration for parameter ‘__wcstold_internal’
but no such parameter
/usr/include/wchar.h:507: error: declaration for parameter ‘__wcstof_internal’ b
ut no such parameter
/usr/include/wchar.h:504: error: declaration for parameter ‘__wcstod_internal’ b
ut no such parameter
/usr/include/wchar.h:414: error: declaration for parameter ‘wcstoul’ but no such
parameter
/usr/include/wchar.h:408: error: declaration for parameter ‘wcstol’ but no such
parameter
/usr/include/wchar.h:394: error: declaration for parameter ‘wcstod’ but no such
parameter
/usr/include/wchar.h:359: error: declaration for parameter ‘wcsrtombs’ but no su
ch parameter
/usr/include/wchar.h:353: error: declaration for parameter ‘mbsrtowcs’ but no su
ch parameter
/usr/include/wchar.h:318: error: declaration for parameter ‘mbrlen’ but no such
parameter
/usr/include/wchar.h:316: error: declaration for parameter ‘__mbrlen’ but no suc
h parameter
/usr/include/wchar.h:312: error: declaration for parameter ‘wcrtomb’ but no such
parameter
/usr/include/wchar.h:308: error: declaration for parameter ‘mbrtowc’ but no such
parameter
/usr/include/wchar.h:298: error: declaration for parameter ‘wctob’ but no such p
arameter
/usr/include/wchar.h:294: error: declaration for parameter ‘btowc’ but no such p
arameter
/usr/include/wchar.h:279: error: declaration for parameter ‘wmemset’ but no such
parameter
/usr/include/wchar.h:276: error: declaration for parameter ‘wmemmove’ but no suc
h parameter
/usr/include/wchar.h:271: error: declaration for parameter ‘wmemcpy’ but no such
parameter
/usr/include/wchar.h:267: error: declaration for parameter ‘wmemcmp’ but no such
parameter
/usr/include/wchar.h:262: error: declaration for parameter ‘wmemchr’ but no such
parameter
/usr/include/wchar.h:243: error: declaration for parameter ‘wcslen’ but no such
parameter
/usr/include/wchar.h:240: error: declaration for parameter ‘wcstok’ but no such
parameter
/usr/include/wchar.h:235: error: declaration for parameter ‘wcsstr’ but no such
parameter
/usr/include/wchar.h:232: error: declaration for parameter ‘wcspbrk’ but no such
parameter
/usr/include/wchar.h:229: error: declaration for parameter ‘wcsspn’ but no such
parameter
/usr/include/wchar.h:225: error: declaration for parameter ‘wcscspn’ but no such
parameter
/usr/include/wchar.h:211: error: declaration for parameter ‘wcsrchr’ but no such
parameter
/usr/include/wchar.h:208: error: declaration for parameter ‘wcschr’ but no such
parameter
/usr/include/wchar.h:183: error: declaration for parameter ‘wcsxfrm’ but no such
parameter
/usr/include/wchar.h:178: error: declaration for parameter ‘wcscoll’ but no such
parameter
/usr/include/wchar.h:153: error: declaration for parameter ‘wcsncmp’ but no such
parameter
/usr/include/wchar.h:150: error: declaration for parameter ‘wcscmp’ but no such
parameter
/usr/include/wchar.h:146: error: declaration for parameter ‘wcsncat’ but no such
parameter
/usr/include/wchar.h:142: error: declaration for parameter ‘wcscat’ but no such
parameter
/usr/include/wchar.h:138: error: declaration for parameter ‘wcsncpy’ but no such
parameter
/usr/include/wchar.h:134: error: declaration for parameter ‘wcscpy’ but no such
parameter
/usr/include/wchar.h:95: error: declaration for parameter ‘mbstate_t’ but no suc
h parameter
/usr/include/ctype.h:151: error: declaration for parameter ‘_tolower’ but no suc
h parameter
/usr/include/ctype.h:150: error: declaration for parameter ‘_toupper’ but no suc
h parameter
/usr/include/ctype.h:146: error: declaration for parameter ‘toascii’ but no such
parameter
/usr/include/ctype.h:142: error: declaration for parameter ‘isascii’ but no such
parameter
/usr/include/ctype.h:119: error: declaration for parameter ‘toupper’ but no such
parameter
/usr/include/ctype.h:116: error: declaration for parameter ‘tolower’ but no such
parameter
/usr/include/ctype.h:112: error: declaration for parameter ‘isxdigit’ but no suc
h parameter
/usr/include/ctype.h:111: error: declaration for parameter ‘isupper’ but no such
parameter
/usr/include/ctype.h:110: error: declaration for parameter ‘isspace’ but no such
parameter
/usr/include/ctype.h:109: error: declaration for parameter ‘ispunct’ but no such
parameter
/usr/include/ctype.h:108: error: declaration for parameter ‘isprint’ but no such
parameter
/usr/include/ctype.h:107: error: declaration for parameter ‘isgraph’ but no such
parameter
/usr/include/ctype.h:106: error: declaration for parameter ‘islower’ but no such
parameter
/usr/include/ctype.h:105: error: declaration for parameter ‘isdigit’ but no such
parameter
/usr/include/ctype.h:104: error: declaration for parameter ‘iscntrl’ but no such
parameter
/usr/include/ctype.h:103: error: declaration for parameter ‘isalpha’ but no such
parameter
/usr/include/ctype.h:102: error: declaration for parameter ‘isalnum’ but no such
parameter
/usr/include/ctype.h:86: error: declaration for parameter ‘__ctype_toupper_loc’
but no such parameter
/usr/include/ctype.h:84: error: declaration for parameter ‘__ctype_tolower_loc’
but no such parameter
/usr/include/ctype.h:82: error: declaration for parameter ‘__ctype_b_loc’ but no
such parameter
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/include/stdarg.h:105: error: declaration
for parameter ‘va_list’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/objimpl.h:259: e
rror: declaration for parameter ‘PyGC_Head’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:523: er
ror: declaration for parameter ‘_Py_HashSecret_t’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:429: er
ror: declaration for parameter ‘PyHeapTypeObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:411: er
ror: declaration for parameter ‘PyTypeObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:305: er
ror: declaration for parameter ‘freefunc’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:302: er
ror: declaration for parameter ‘PyBufferProcs’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:293: er
ror: declaration for parameter ‘PyMappingMethods’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:287: er
ror: declaration for parameter ‘PySequenceMethods’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:273: er
ror: declaration for parameter ‘PyNumberMethods’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:179: er
ror: declaration for parameter ‘Py_buffer’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:112: er
ror: declaration for parameter ‘PyVarObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/object.h:108: er
ror: declaration for parameter ‘PyObject’ but no such parameter
/root/Asta/nastassiasaidakcython/Cython-0.22.1/Python27/include/pymem.h:52: erro
r: declaration for parameter ‘PyMem_Malloc’ but no such parameter
module.c:10286: error: expected ‘{’ at end of input
我是在正确的轨道上,还是这些错误表明一个更严重的问题,它告诉我需要找到一种不同的方法?
答案 0 :(得分:2)
How can I make a standalone binary from a Python program using cython?
你可能想要一个像这样的食谱:
PYVERSION=2.7
foobar: foobar.py
cython --embed foobar.py -o foobar.c
$(CC) -I /usr/include/python$(PYVERSION) foobar.c -lpython$(PYVERSION) -o foobar
魔术是--embed选项,它在生成的C中嵌入了Python解释器main的副本。你想要改变foobar'当然,要反映你的剧本名称和PYVERSION。
你可能不需要--embed开始尝试创建一个库,但是你确实需要-lpython参数来告诉它链接哪个共享库。