如何使用Cython创建一个stand dll [英] How to use Cython to create a stand dll

查看:841
本文介绍了如何使用Cython创建一个stand dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,我将脚本重命名为.pyx文件。我想将这段代码编译成一个stand dll文件。



我在这个文件,Cython将创建一个dll文件,但是我只得到一个pyd。



我已经打了使用命令 python setup.py build --compiler = mingw32 编译脚本我的代码(只是一个你好世界):



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $


$ $ b

任何想法?谢谢

解决方案

* .pyd文件与DLL相同



是的,.pyd文件是dll的,但有一些区别。如果你有一个名为 foo.pyd 的DLL,那么它必须有一个函数 initfoo()。然后,您可以编写Pythonimport foo,Python将搜索foo.pyd(以及foo.py,foo.pyc),如果找到它,将尝试调用 initfoo()来初始化它。您不要将.exe与foo.lib链接,因为这将导致Windows要求DLL存在。



请注意,foo.pyd的搜索路径是PYTHONPATH,与Windows用于搜索foo.dll的路径不同。此外,foo.pyd不需要存在运行您的程序,而如果您将程序与dll链接,那么dll是必需的。当然,如果你想说 import foo ,则需要foo.pyd。在DLL中,链接在源代码中用 __ declspec(dllexport)声明。在.pyd中,链接在可用函数列表中定义。



修改Python的搜索路径绝对和相对导入


I have a Python script, I renamed the script to .pyx file. I want compile this code to a stand dll file.

I saw in this document that Cython will create a dll file, but i only get a pyd.

I have mingw and try to use command python setup.py build --compiler=mingw32 to compile the script my code (just a hello world):

def init():
    return "hello world"

Any ideas? Thanks

解决方案

Is a *.pyd file the same as a DLL?

Yes, .pyd files are dll’s, but there are a few differences. If you have a DLL named foo.pyd, then it must have a function initfoo(). You can then write Python "import foo", and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call initfoo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.

Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say import foo. In a DLL, linkage is declared in the source code with __declspec(dllexport). In a .pyd, linkage is defined in a list of available functions.

Modifying Python’s Search Path, Absolute and Relative Imports

这篇关于如何使用Cython创建一个stand dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆