簡體   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