我正在尝试从 Python 代码构建独立的应用程序。目前它只是一个“hello world”程序。我用 Cython 编译它以获得一个 .c 文件:
"c:\python34\scripts\cython.exe" --embed hello.py
这很好用。然后我尝试按如下方式编译和链接生成的 .c 文件:
"c:\mingw32\bin\gcc.exe" -I"c:\python34\include" -L"c:\python34\libs" -lpython34 -ohello.exe hello.c
这给了我很多链接错误:
...\cc7PmSei.o:hello.c:(.text+0xe9): undefined reference to `_imp__PyTuple_New'
...\cc7PmSei.o:hello.c:(.text+0x130): undefined reference to `_imp__PyBytes_FromStringAndSize'
...\cc7PmSei.o:hello.c:(.text+0x177): undefined reference to `_imp__PyModule_Create2'
...
...\cc7PmSei.o:hello.c:(.text+0x12b7): undefined reference to `_imp__PyUnicode_Decode'
...\cc7PmSei.o:hello.c:(.text+0x12dd): undefined reference to `_imp__PyUnicode_FromStringAndSize'
...\cc7PmSei.o:hello.c:(.text+0x1303): undefined reference to `_imp__PyBytes_FromStringAndSize'
.../libmingw32.a(main.o):main.c:.text.startup+0xa7): undefined reference to `WinMain@16'
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 位。
我做了一些研究。有人说这可能是由于使用 32 位 C 编译器和 64 位 Python 引起的。但这里不是这种情况。其他 ( http://eli.thegreenplace.net/2008/06/28/compiling-python-extensions-with-distutils-and-mingw/) 说我需要创建 libpython34.a。但是我的 Python 版本已经附带了这个文件。
有人知道我做错了什么吗?提前致谢。
最佳答案
在hello.c中找到:
#if PY_MAJOR_VERSION < 3
int main(int argc, char** argv) {
#elif defined(WIN32) || defined(MS_WINDOWS)
int wmain(int argc, wchar_t **argv)
并将 wmain 替换为 main。这对我有用。
关于python - 使用 Cython + MinGW 构建独立应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219012/