Python 设置脚本扩展,如何包含 .h 文件?

发布于 2024-10-03 20:30:03 字数 776 浏览 6 评论 0原文

所以我有一个看起来像这样的目录:

 home\
     setup.py
     some_python_file.py
     ext\
         __init__.py
         c_file1.c
         c_file2.c
         ext_header.h

显然头文件是编译 c 文件所必需的,但问题是我无法让安装脚本包含头文件。

我的扩展对象是这样的:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c'])

它可以工作,但不包括头文件。如果我将其更改为:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c', 'ext_header.h'])

它包含“.h”文件,但当我运行安装时不会构建。相反,它给出错误错误:未知文件类型'.h'(来自'ext/ext_header.h')

如果我将头文件作为数据文件包含在内:

data_files=[('ext', ['ext/ext_header.h'])]

它不起作用总之,.h 文件甚至没有进入 MANIFEST 文件。

所以我的问题是,如何将此扩展包含在头文件中,以便 python setup.py install 能够正确构建它?

So I've got a directory that looks something like this:

 home\
     setup.py
     some_python_file.py
     ext\
         __init__.py
         c_file1.c
         c_file2.c
         ext_header.h

Obviously the header file is necessary to compile the c files, but the problem is that I can't get the setup script to include the header file.

My extension object is something like this:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c'])

Which works, but doesn't include the header file. If I change it to:

Extension('ext.the_extension', ['ext/c_file1.c', 'ext/c_file2.c', 'ext_header.h'])

It includes the '.h' file but then doesn't build when I run install. Instead it gives and error error: unknown file type '.h' (from 'ext/ext_header.h')

If I include the header file as a data file like this:

data_files=[('ext', ['ext/ext_header.h'])]

it doesn't work at all, the .h file doesn't even make it into the MANIFEST file.

So my queustion is, how do you include this extension with the header file so that python setup.py install will build it correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鲜肉鲜肉永远不皱 2024-10-10 20:30:03

我有一种感觉 pyfunc 正在寻求一个更标准的解决方案,但我自己确实找到了另一个解决方案。我不知道这是一个好的解决方案还是只是一个黑客,但我所做的只是将头文件添加到 MANIFEST.in 中。该文档并没有真正让人觉得这就是 MANIFEST.in 文件的用途,但它确实有效。我的 MANIFEST.in 文件现在如下所示:

include ext/ext_header.h

其中包含该文件,并在我运行 python setup.py install 时成功编译

I have a feeling pyfunc is on track for a more standard solution, but I did find another solution on my own. I have no idea if this is a good solution or just a hack, but all I did is add the header file to the MANIFEST.in. The documentation doesn't really make it seem like this is what the MANIFEST.in file is for, but it does work. My MANIFEST.in file now looks like this:

include ext/ext_header.h

Which includes the file and sucessfully compiles when I run python setup.py install

梦境 2024-10-10 20:30:03

从文档中,

module1 = Extension('demo',
                define_macros = [('MAJOR_VERSION', '1'),
                                 ('MINOR_VERSION', '0')],
                include_dirs = ['/usr/local/include'],
                libraries = ['tcl83'],
                library_dirs = ['/usr/local/lib'],
                sources = ['demo.c'])

您应该通过“include_dirs”提供包含文件。

为什么这对你不起作用?

From the docs,

module1 = Extension('demo',
                define_macros = [('MAJOR_VERSION', '1'),
                                 ('MINOR_VERSION', '0')],
                include_dirs = ['/usr/local/include'],
                libraries = ['tcl83'],
                library_dirs = ['/usr/local/lib'],
                sources = ['demo.c'])

You should provide the include files via "include_dirs".

Why does this not work for you?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文