问题描述
我正在尝试从Python代码构建独立的应用程序.目前,这只是一个"hello world"程序.我用Cython编译它以获得一个.c文件:
I'm trying to build standalone application from Python code. At the moment it is only a "hello world" program. I compile it with Cython to get a .c file:
那很好.然后,我尝试按如下所示编译并链接生成的.c文件:
That works fine. Then I try to compile and link the generated .c file as follows:
这给了我很多链接错误:
That gives me a whole lot of link errors:
... \ cc7PmSei.o:hello.c :(.text + 0x130):对`_imp__PyBytes_FromStringAndSize'的未定义引用
...\cc7PmSei.o:hello.c:(.text+0x130): undefined reference to `_imp__PyBytes_FromStringAndSize'
... \ cc7PmSei.o:hello.c :(.text + 0x177):对'_imp__PyModule_Create2'的未定义引用
...\cc7PmSei.o:hello.c:(.text+0x177): undefined reference to `_imp__PyModule_Create2'
...
... \ cc7PmSei.o:hello.c :(.text + 0x12b7):对'_imp__PyUnicode_Decode'的未定义引用
...\cc7PmSei.o:hello.c:(.text+0x12b7): undefined reference to `_imp__PyUnicode_Decode'
... \ cc7PmSei.o:hello.c :(.text + 0x12dd):对`_imp__PyUnicode_FromStringAndSize'的未定义引用
...\cc7PmSei.o:hello.c:(.text+0x12dd): undefined reference to `_imp__PyUnicode_FromStringAndSize'
... \ cc7PmSei.o:hello.c :(.text + 0x1303):对`_imp__PyBytes_FromStringAndSize'的未定义引用
...\cc7PmSei.o:hello.c:(.text+0x1303): undefined reference to `_imp__PyBytes_FromStringAndSize'
.../libmingw32.a(main.o):main.c:.text.startup + 0xa7):对WinWin @ 16的未定义引用
.../libmingw32.a(main.o):main.c:.text.startup+0xa7): undefined reference to `WinMain@16'
collect2.exe:错误:ld返回1个退出状态
collect2.exe: error: ld returned 1 exit status
更多信息:我有Windows 7 Home 64位操作系统.我使用的是Python 3.4.1 32位,Cython-0.20.1和TDM-GCC 4.7.1 32位.
Some more info: I have Windows 7 Home 64-bit OS. I use Python 3.4.1 32-bit, Cython-0.20.1, and TDM-GCC 4.7.1 32-bit.
我做了一些研究.有人说这可能是由于使用32位C编译器和64位Python引起的.但这不是事实.其他( http://eli.thegreenplace.net/2008 /06/28/compiling-python-extensions-with-distutils-and-mingw/)说我需要创建libpython34.a.但是我的Python版本已经包含了此文件.
I did some research. Some people say that it can be caused for example by using 32-bit C compiler and 64-bit Python. But it is not the case here. Other (http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/) say that I need to create libpython34.a. But my version of Python already came with this file.
有人知道我做错了什么吗?预先感谢.
Does anyone have an idea what I do wrong? Thanks in advance.
推荐答案
好.我想到了.这里有两个问题:
Ok. I figured it out. There are two problems here:
首先,未定义对"WinMain @ 16"的引用是由于Cython为Python 3生成了"wmain"而不是"main".在MinGW中有一个"-municode"命令行选项来支持"wmain". ",但看起来它仅在最新的64位版本的MinGW中实现.我已安装的MinGW 4.8.1 32位未实现该功能.另一种选择是在C语言中使用包装器"main"函数,或者仅使用Python 2.7.我选择了后者.
First, undefined reference to 'WinMain@16' is due to to the fact that Cython generates 'wmain' instead of 'main' for Python 3. There is a '-municode' command line option in MinGW to support 'wmain', but it looks that it is only implemented in the latest 64-bit versions of MinGW. It is not implemented in MinGW 4.8.1 32-bit I have installed. An alternative is to use a wrapper 'main' function in C, or just use Python 2.7. I chose the latter.
剩余的未定义引用归因于错误的参数顺序.我可以使用以下命令来构建应用程序:
The remaining undefined references are due to a wrong order of parameters. I can build the application just fine with the following command:
这篇关于使用Cython + MinGW构建独立的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!