【问题标题】:Building standalone application with Cython + MinGW使用 Cython + MinGW 构建独立应用程序
【发布时间】:2014-06-14 10:33:27
【问题描述】:

我正在尝试从 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): 对 `_imp__PyTuple_New' 的未定义引用

...\cc7PmSei.o:hello.c:(.text+0x130): 对`_imp__PyBytes_FromStringAndSize'的未定义引用

...\cc7PmSei.o:hello.c:(.text+0x177): 对`_imp__PyModule_Create2'的未定义引用

...

...\cc7PmSei.o:hello.c:(.text+0x12b7): 对`_imp__PyUnicode_Decode'的未定义引用

...\cc7PmSei.o:hello.c:(.text+0x12dd): 对 `_imp__PyUnicode_FromStringAndSize' 的未定义引用

...\cc7PmSei.o:hello.c:(.text+0x1303): 对`_imp__PyBytes_FromStringAndSize'的未定义引用

.../libmingw32.a(main.o):main.c:.text.startup+0xa7): 未定义对 `WinMain@16' 的引用

collect2.exe:错误:ld 返回 1 个退出状态

更多信息:我有 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 版本已经附带了这个文件。

有人知道我做错了什么吗?提前致谢。

【问题讨论】:

    标签: python mingw cython


    【解决方案1】:

    在 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。这对我有用。

    【讨论】:

      【解决方案2】:

      好的。我想到了。这里有两个问题:

      首先,对“WinMain@16”的未定义引用是因为 Cython 为 Python 3 生成“wmain”而不是“main”。MinGW 中有一个“-municode”命令行选项来支持“wmain” ',但看起来它只在最新的 64 位版本的 MinGW 中实现。它没有在我安装的 MinGW 4.8.1 32 位中实现。另一种方法是在 C 中使用包装器“main”函数,或者只使用 Python 2.7。我选择了后者。

      剩余的未定义引用是由于参数顺序错误造成的。我可以使用以下命令构建应用程序:

      "c:\mingw\bin\gcc.exe" -I"c:\python27\include" -L"c:\python27\libs" hello.c -ohello.exe -lpython27

      【讨论】:

      • 哦!我想从我的 linux 盒子编译一个 windows 盒子
      【解决方案3】:

      在 mingw-w64(来自 win-builds.org 的 32 位版本)中,gcc-4.8.1 具有 -municode 选项,它将使用 wmain() 作为入口点,UTF-16 命令行也可以正常工作。很遗憾,官方文档中的任何地方都没有提到它。经过三天的实验,在一些irc日志上找到了它:)

      【讨论】:

        猜你喜欢
        • 2010-09-06
        • 2018-09-21
        • 2017-12-09
        • 2011-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 2011-05-27
        • 2016-02-27
        相关资源
        最近更新 更多