繁体   English   中英

setuptools:从 C++ 代码构建共享库,然后构建链接到共享库的 Cython 包装器

[英]setuptools: build shared library from C++ code, then build Cython wrapper linked to shared library

我们有一堆带有类的 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文件生成的库。 所有这些都可以使用makecmake轻松完成,但我们想要一个仅使用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功能可以做到这一点,例如:

import os

from setuptools import setup
from Cython.Build import cythonize

ext_lib_path = 'rectangle'
include_dir = os.path.join(ext_lib_path, 'include')

sources = ['Rectangle.cpp']

# Use as macros = [('<DEFINITION>', '<VALUE>')]
# where value can be None
macros = None

ext_libraries = [['rectangle', {
               'sources': [os.path.join(ext_lib_path, src) for src in sources],
               'include_dirs': [include_dir],
               'macros': macros,
               }
]]

extensions = [Extension("rect",
              sources=["rect.pyx"],
              language="c++",
              include_dirs=[include_dir],
              libraries=['rectangle'],
)]

setup(ext_modules=cythonize(extensions),
      libraries=ext_libraries)

libraries参数构建在目录rectangle找到的外部库,包含目录rectangle/include在它和扩展之间是通用的。

setuptools推荐使用的distutils切换到setuptools ,现在是 setuptools 的一部分。

没有看到关于这个论点的任何文档,但看到它在其他项目中使用。

这是未经测试的,如果它不起作用,请提供示例文件进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM