5

我正在为我想作为 Python 包分发的 C++ 库编写 Cython 包装器。我想出了一个看起来像这样的包的虚拟版本(完整源代码here)。

$ tree
.
├── bogus.pyx
├── inc
│   └── bogus.hpp
├── setup.py
└── src
    └── bogus.cpp
$
$ cat inc/bogus.hpp 
#ifndef BOGUS
#define BOGUS

class 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=ascii

cdef 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, Extension
import glob

headers = 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++ 头文件与源代码分发捆绑在一起?!?!

<咆哮>

对此进行故障排除后,我们发现了大量令人费解且不一致的文档、建议和技巧,但这些都对我没有用。放一条graft线MANIFEST.in?没有。或选项package_datadata_files没有。在过去的几年里,Python 打包似乎有了很大的改进,但对于我们这些不依赖 Python 打包的人来说,它仍然几乎是难以理解的!

</rant>

4

1 回答 1

3

简短的回答

放入文件include inc/*.hppMANIFEST.in

长答案

根据各种博客文章和 SO 线程,我尝试了在文件中声明文件的建议MANIFEST.in。按照这些说明,我添加了graft inc/一行MANIFEST.in来包含整个目录。这没有用。

但是,将这条线替换为include inc/*.hpp确实有效。可以说这应该是我尝试的第一件事,但是由于不熟悉 setuptools 和 distutils 的复杂性和缺陷,我没有理由期望这graft不会奏效。

于 2016-09-27T22:47:31.553 回答