无法将cython生成的C语言代码转换为可执行文件 [英] Unable to convert cython generated C language code to executable file
问题描述
我正在使用Windows操作系统。我有Visual Studio和与其相关的所有构建工具。我有简单的python脚本。我用cython生成了一个 C文件。我正在尝试将此C文件转换为可执行文件。我不想使用任何其他模块,例如pyinstaller,py2exe或cxFreeze。
I am using windows operating system. I have visual studio and all the build tools associated with it. I have simple python script. I used cython to generate a "C" file. I am trying to convert this C file to an executable. I do not want to use any other modules like pyinstaller, py2exe or cxFreeze.
从.py到C的转换成功。但是可悲的是,在将此C文件转换为可执行文件时遇到一些错误。
The conversion from .py to C was successful. However sadly I am getting some errors while converting this C file to an executable.
有人可以帮助我吗?我已经检查了有关此问题的其他各种问题,但都没有这个问题。
Could anyone please help me ? I have checked various other questions on this issue and none of have them have this issue.
这是我的(.pyx脚本)
from tkinter import Tk
root = Tk()
root.mainloop()
我在CMD中使用以下命令为该脚本构建C扩展名:
python setup.py build_ext --inplace -DMS_WIN64
然后我在CMD中使用此命令来构建可执行文件:
gcc -DMS_WIN64 C:/Users/Siva/Desktop/Scripts/TEST/test.c -IC:/"Program Files"/Python37/include -LC:/"Program Files"/Python37/libs -lpython37 -o output
这会产生以下错误:
C:\Users\Siva\Desktop\Scripts\TEST>gcc -DMS_WIN64 C:/Users/Siva/Desktop/Scripts/TEST/test.c -IC:/"Program Files"/Python37/include -LC:/"Program Files"/Python37/libs -lpython37.a -o output
C:/Users/Siva/Desktop/Scripts/TEST/test.c:213:41: warning: division by zero [-Wdiv-by-zero]
enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
^
C:/Users/Siva/Desktop/Scripts/TEST/test.c:213:12: error: enumerator value for '__pyx_check_sizeof_voidp' is not an integer constant
enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
^~~~~~~~~~~~~~~~~~~~~~~~
我已附上以下错误的屏幕截图
推荐答案
最后经过长期的永恒斗争,我找到了解决方案。感谢约书亚的大力支持。
Finally After a long eternal struggle I have found the solution. Thanks to Joshua for the great support.
.PYX代码进行了少量修改。看来我们必须在函数中定义所有内容,以便当代码转换为C语言时,它具有主要功能。
There is a small modification in the .PYX code. It appears like we must define everything within a function so that when the code gets converted to C language, it has a main function in it.
因此第一步是将.PYX编辑为:
from tkinter import Tk
cdef public void function():
root = Tk()
root.mainloop()
if __name__ == "__main__":
function()
然后,我使用以下命令生成了头文件(.h)和C程序文件
python -m cython test.pyx --embed
这就是窍门:
转到生成的C程序文件,搜索wmain函数并从中删除 w。或者,您也可以通过Ctrl + F搜索wmain并将其实例替换为main。
在我的程序中,例如只有一个wmain实例。我将其更改为main。它看起来像下面显示的代码。
#if PY_MAJOR_VERSION < 3
int main(int argc, char** argv) {
#elif defined(WIN32) || defined(MS_WINDOWS)
int wmain(int argc, wchar_t **argv)
然后,我使用GCC命令生成可执行文件。命令如下。我不明白为什么会这样。
x86_64-w64-mingw32-gcc-8.1.0 -mconsole -DSIZEOF_VOID_P=8 -DMS_WIN64 C:/Users/Siva/Desktop/Scripts/CYTHON/test.c -IC:/"Program Files"/Python37/include -LC:/"Program Files"/Python37/libs -lpython37 -o output
我只希望有人能解释为什么这种方法有效。
I only wish someone would explain why approach works.
这篇关于无法将cython生成的C语言代码转换为可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!