繁体   English   中英

如何在 Python 扩展模块的 setup.py 脚本中指定 header 文件?

[英]How to specify header files in setup.py script for Python extension module?

如何在 Python 扩展模块的 setup.py 脚本中指定 header 文件? 按如下方式将它们与源文件一起列出是行不通的。 但我不知道在哪里列出它们。

from distutils.core import setup, Extension
from glob import glob

setup(
    name = "Foo",
    version = "0.1.0",
    ext_modules = [Extension('Foo', glob('Foo/*.cpp') + glob('Foo/*.h'))]
)

在 setup.py 之外添加MANIFEST.in文件,内容如下:

graft relative/path/to/directory/of/your/headers/

我在使用 setuptools 时遇到了很多麻烦,它甚至不再有趣了。 以下是我最终不得不使用一种解决方法来使用 header 文件生成工作源分发的方式:我使用了 package_data。

我分享这个是为了有可能避免其他人的恶化。 如果您知道更好的工作解决方案,请告诉我。

有关详细信息,请参见此处: https://bitbucket.org/blais/beancount/src/ccb3721a7811a042661814a6778cca1c42433d64/setup.py?fileviewer=file-view-default#setup.py-36

    # A note about setuptools: It's profoundly BROKEN.
    #
    # - The header files are needed in order to distribution a working
    #   source distribution.
    # - Listing the header files under the extension "sources" fails to
    #   build; distutils cannot make out the file type.
    # - Listing them as "headers" makes them ignored; extra options to
    #   Extension() appear to be ignored silently.
    # - Listing them under setup()'s "headers" makes it recognize them, but
    #   they do not get included.
    # - Listing them with "include_dirs" of the Extension fails as well.
    #
    # The only way I managed to get this working is by working around and
    # including them as "packaged data" (see {63fc8d84d30a} below). That
    # includes the header files in the sdist, and a source distribution can
    # be installed using pip3 (and be built locally). However, the header
    # files end up being installed next to the pure Python files in the
    # output. This is the sorry situation we're living in, but it works.

我的OSS项目里有对应的ticket: https://bitbucket.org/blais/beancount/issues/72

如果我没记错的话,你应该只需要指定源文件,它应该找到/使用标题。

在 setup-tools 手册中,我看到了一些我相信的东西。

“例如,如果您的扩展需要 header 文件位于您的分发根目录下的包含目录中,请使用 include_dirs 选项”

Extension('foo', ['foo.c'], include_dirs=['include'])

http://docs.python.org/distutils/setupscript.html#preprocessor-options

尝试将标头 kwarg 设置为 setup()。 我不知道它在任何地方都有记录,但它确实有效。

setup(name='mypkg', ..., headers=['src/includes/header.h'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM