我正在为我想作为 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_data
?data_files
没有。在过去的几年里,Python 打包似乎有了很大的改进,但对于我们这些不依赖 Python 打包的人来说,它仍然几乎是难以理解的!
</rant>