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

How to set the include path in a pybind11 project

本文关键字:设置 包含 路径 项目 pybind11      更新时间:2023-10-16

我有一个大型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安装./package_name时,我收到C++编译器错误,因为找不到库头。 我尝试将"include_dirs"参数更改为扩展以包含几个不同的 ../../..到达项目目录的顶部,但没有成功。

我宁愿不将完整的库复制到 .../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)