我们有一组C++类的文件,我们使用Cython将它们包到Python。我们使用setuptools构建Cython扩展。这一切都很好,我们遵循以下指南: http://cython.readthedocs.io/en/latest/src/userguide/wrapping_CPlusPlus.html
我们基本上是这样做的
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(
"rect.pyx", # our Cython source
sources=["Rectangle.cpp"], # additional source file(s)
language="c++", # generate C++ code
))
我们不喜欢这样,我们必须重新编译所有内容,即使只有Cython部分发生了更改,rect.pyx
在本例中。事实上,我们从不接触.cpp
文件,而是经常更改.pyx
文件
我们希望将.cpp
文件分别编译成静态或共享库,然后独立构建.pyx
文件,该文件链接到从.cpp
文件生成的库。使用make
或cmake
这一切都很容易,但我们需要一个只使用setuptools
的纯Python解决方案。模拟代码如下所示:
from distutils.core import setup
from Cython.Build import cythonize
class CppLibary:
# somehow get that to work
# this should only recompile cpplib when source files changed
cpplib = CppLibary('cpplib',
sources=["Rectangle.cpp"], # put static cpp code here
include_dirs=["include"])
setup(ext_modules = cythonize(
"rect.pyx", # our Cython source
libraries=[cpplib], # link to cpplib
language="c++", # generate C++ code
))
setup
的一个看似未记录的功能可以做到这一点,例如:libraries
参数构建在目录rectangle
中找到的外部库,在它和扩展之间使用include目录rectangle/include
公共还将导入从
distutils
切换到setuptools
,该导入已被弃用,现在是setuptools的一部分我还没有看到关于这个论点的任何文档,但看到它在其他项目中使用过
这是未经测试的,如果不起作用,请提供样本文件进行测试
相关问题 更多 >
编程相关推荐