如何在 pybind11 项目中设置包含路径

How to set the include path in a pybind11 project

我有一个大型 C++ 库,我正尝试使用 pybind11 公开它。我无法正确设置包含路径。

项目目录结构如下:

root
- sub-project-1
  -> C++ files that know nothing about python.
- sub-project-2
  -> more C++ files
  -> sub-sub-project
     -> even more C++ files
- sub-project-3
  -> et cetera
- Interfaces
  -> R
     -> R interface stuff
  -> python
     -> package_name
        -> setup.py
        -> pybind11_bindings.cpp 

setup.py 文件当前如下所示。该结构主要是从pybind11文档中复制的。

    from setuptools import setup, Extension
    from setuptools.command.build_ext import build_ext
    import sys
    import setuptools
    from glob import glob

    __version__ = '0.0.1'


    class get_pybind_include(object):
        """Helper class to determine the pybind11 include path

        The purpose of this class is to postpone importing pybind11
        until it is actually installed, so that the ``get_include()``
        method can be invoked. """

        def __init__(self, user=False):
            self.user = user

        def __str__(self):
            import pybind11
            return pybind11.get_include(self.user)

    # omitting long lists of files.
    my_sources = list_of_all_project_cpp_files + list_of_all_pybind11_binding_files

    ext_modules = [
        Extension(
            'Boom',
            sources=my_sources,
            include_dirs=[
                "../..",
                # Path to pybind11 headers
                get_pybind_include(),
                get_pybind_include(user=True)
            ],
            language='c++'
        ),
    ]
    # some other stuff...
    setup(
        name='package_name',
        version=__version__,
        author='Me',
        author_email='my.email@gmail.com',
        url='https://a/url/to/somewhere',
        description='description goes here.',
        ext_modules=ext_modules,
        install_requires=['pybind11>=2.3'],
        setup_requires=['pybind11>=2.3'],
        cmdclass={'build_ext': BuildExt},
        zip_safe=False,
    )

当我 pip3 install ./package_name 时,我收到 C++ 编译器错误,因为找不到库头文件。我尝试将 'include_dirs' 参数更改为 Extension 以包含几个不同的层 ../../.. 以到达项目目录的顶部,但没有成功。

我不想将完整的库复制到 .../python/project_name 目录中,除非这里的专家告诉我我需要这样做。

缺少的包含文件需要通过创建 MANIFEST.in 文件包含在包中。

# This file lists all the header files to include with the distribution.
# See https://packaging.python.org/guides/using-manifest-in/ for syntax.

(commands using 'include' 'recursive-include' and 'graft' go here)