python - 使用 Cython + MinGW 构建独立应用程序

标签 python mingw cython

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

相关文章:

python - 为什么 pandas 在简单的数学运算上比 numpy 更快?

C++11;非静态数据成员初始化可以访问其他数据成员吗?

python - 赛通 "Cannot assign default value to fields in cdef classes, structs or unions"

python - C(P)ython 或 D 中的多平台 gui 应用程序

python - subprocess.call() 中的 Pytest 模拟全局变量

C++ 运行时类型与 Python 模块不匹配?

python - 我们可以像在 php 中一样在 python 中生成网页吗?

c++ - MinGW g++ 在 powershell 中不创建输出

c++ - 如何使用 `static linking` 和 `Dynamic linking` 以及 gcc 和 Visual Studio 构建 C/C++ 程序?

检查一个值是否存在于 Cython 的数组中