使用Python包源代码分发捆绑C扩展头

使用Python包源代码分发捆绑C扩展头,第1张

概述我正在编写一个Cython包装器到C库,我想将其作为 Python包分发.我想出了我的包的虚拟版本,看起来像这样(完整源码 here). $tree.├── bogus.pyx├── inc│   └── bogus.hpp├── setup.py└── src └── bogus.cpp$$cat inc/bogus.hpp #ifndef BOGUS#define 我正在编写一个Cython包装器到C库,我想将其作为 Python包分发.我想出了我的包的虚拟版本,看起来像这样(完整源码 here).

$tree.├── bogus.pyx├── inc│   └── bogus.hpp├── setup.py└── src    └── bogus.cpp$$cat inc/bogus.hpp #ifndef BOGUS#define BOGUSclass bogus{protected:    int data;public:    bogus();    int get_double(int value);};#endif$$cat src/bogus.cpp #include "bogus.hpp"bogus::bogus() : data(0){}int bogus::get_double(int value){    data = value * 2;    return data;}$cat bogus.pyx # distutils: language = c++# distutils: sources = src/bogus.cpp# cython: c_string_type=str,c_string_enCoding=asciicdef extern from 'bogus.hpp':    cdef cppclass bogus:        bogus() except +        int get_double(int value)cdef class Bogus:    cdef bogus b    def get_double(self,int value):        return self.b.get_double(value)

使用以下setup.py文件,我可以确认使用python setup.py install正确安装库,并且它可以正常工作.

from setuptools import setup,Extensionimport globheaders = List(glob.glob('inc/*.hpp'))bogus = Extension(    'bogus',sources=['bogus.pyx','src/bogus.cpp'],include_dirs=['inc/'],language='c++',extra_compile_args=['--std=c++11','-Wno-unused-function'],extra_link_args=['--std=c++11'],)setup(    name='bogus',description='Troubleshooting Python packaging and distribution',author='DanIEl Standage',ext_modules=[bogus],install_requires=['cython'],version='0.1.0')

但是,当我使用python setup.py sdist build构建源代码分发时,不包含C头文件,并且无法编译C扩展.

如何确保C头文件与源代码分发捆绑在一起?!?!

<咆哮>

对此进行故障排除发现了一个非常令人费解和不一致的文档,建议和黑客的混乱,其中没有一个对我有用.在MANIFEST.in中放置一条移植线?不. package_data或data_files选项?不.在过去的几年中,Python包装似乎已经有了很大的改进,但对于我们这些没有生活和呼吸Python包装的人来说,它仍然是难以穿透的!

< /咆哮>

解决方法 简短的回答

在MANIFEST.in文件中包含inc / * .hpp.

答案很长

基于各种博客文章和SO线程,我尝试了在MANIFEST.in文件中声明文件的建议.在these instructions之后,我向MANIFEST.in添加了一个移植公司/行,以包含整个目录.这没用.

但是,用include inc / * .hpp替换这一行确实有效.可以说这应该是我尝试过的第一件事,但由于不熟悉setuptools和distutils的复杂性和瑕疵,我没有理由认为移植不会起作用.

总结

以上是内存溢出为你收集整理的使用Python包源代码分发捆绑C扩展头全部内容,希望文章能够帮你解决使用Python包源代码分发捆绑C扩展头所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1197719.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存