【发布时间】:2019-12-30 06:14: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 docs。
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 目录中,除非这里的专家告诉我需要这样做。
【问题讨论】:
标签: python c++ setuptools pybind11