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

问题描述 投票:0回答:5

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

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'))]
)
python distutils
5个回答
22
投票

除了 setup.py 之外,添加 MANIFEST.in 文件,其中包含以下内容:

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

8
投票

尝试使用 headers kwarg 来 setup()。我不知道它在任何地方都有记录,但它有效。

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

6
投票

我在设置工具方面遇到了很多麻烦,它甚至不再有趣了。 以下是我最终不得不使用解决方法来生成带有头文件的工作源发行版的方法:我使用了 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


4
投票

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

在设置工具手册中,我相信我看到了一些关于此的内容。

“例如,如果您的扩展需要分发根目录下的 include 目录中的头文件,请使用 include_dirs 选项”

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

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


0
投票

我可以确认使用

MANIFEST.in
文件指定包含标头的相对路径是有效的。我有一个名为
kernels
的文件夹,其中包含 4 个 CUDA 内核,每个内核位于一个单独的文件夹中。此外,我还有头文件
utils.h
,它通过使用
#include "../utils.h"
在所有 4 个文件夹之间共享。当我运行命令
python3 -m build
时,头文件
utils.h
未复制到构建文件夹,并且包含语句失败,因为它找不到头文件。最后,使用
MANIFEST.in
文件成功了,我可以看到头文件被复制了。

© www.soinside.com 2019 - 2024. All rights reserved.