python - 通过 cmake 从 C++ 扩展构建 Python 子模块
问题描述
我正在尝试通过 cmake 将 c++ 扩展作为子模块合并到现有的 python 库中。构建 C++ 扩展可以正常工作并将其作为 python 模块导入,但不能作为头库的子模块。
我有以下目录结构:
frontend/
foo.py
bar.py
backend/
backend.cpp
扩展通过 pybind 绑定到一个 python 模块:
PYBIND11_MODULE(backend, m)
{
m.doc() = "backend c++ implementation"; // optional module docstring
m.def("method", &method, "The method I want to call from python.");
}
在 CMakeLists.txt 中,相关行是:
pybind11_add_module(backend "frontend/backend/backend.cpp")
我已按照此处和此处的说明表格编写 setup.py 脚本。我猜最重要的几行如下所示:
from setuptools import setup, Extension, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.test import test as TestCommand
class CMakeExtension(Extension):
def __init__(self, name, sourcedir=".", sources=[]):
Extension.__init__(self, name, sources=[])
class CMakeBuild(build_ext):
def run(self):
build_directory = os.path.abspath(self.build_temp)
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)
cmake_list_dir = os.path.abspath(os.path.dirname(__file__))
print("-" * 10, "Running CMake prepare", "-" * 40)
subprocess.check_call(
["cmake", cmake_list_dir], cwd=self.build_temp,
)
print("-" * 10, "Building extensions", "-" * 40)
cmake_cmd = ["cmake", "--build", "."] + self.build_args
subprocess.check_call(cmake_cmd, cwd=self.build_temp)
# Move from build temp to final position
for ext in self.extensions:
self.move_output(ext)
def move_output(self, ext):
build_temp = Path(self.build_temp).resolve()
dest_path = Path(self.get_ext_fullpath(ext.name)).resolve()
source_path = build_temp / self.get_ext_filename(ext.name)
dest_directory = dest_path.parents[0]
dest_directory.mkdir(parents=True, exist_ok=True)
self.copy_file(source_path, dest_path)
extensions = [CMakeExtension("backend")]
setup(
name="frontend",
packages=["frontend"],
ext_modules=extensions,
cmdclass=dict(build_ext=CMakeBuild),
)
但这并不构成backend
的子模块frontend
,而是独立的模块。所以这有效:
from backend import method
但是为了避免与其他库的命名问题,我想要的是:
from frontend.backend import method
不幸的是,更改 pybinding 或扩展调用中的命名extensions = [CMakeExtension("frontend.backend")]
并不能解决我的问题,然后安装程序找不到backend.<platform>.so
共享库,因为它查找frontend/backend.<platform>.so
不存在的 。我该如何解决这个问题?
解决方案
我想我已经通过以下几行解决了这个问题:
setup.py 文件中的更改:
ext_modules = [
Extension(
"frontend.backend", sources=["frontend/backend/backend.cpp"]
)
]
CMakeLists.txt 文件中的更改:
pybind11_add_module(backend "frontend/backend/backend.cpp")
set_target_properties( backend
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/frontend"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/frontend"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/frontend"
)
共享库对象 backend.platform.so 必须位于 frontend 目录中。pybind 模块名称和源文件 .cpp 都不应包含任何“。” 在名称中,因为get_ext_fullpath()
方法 frombuild_ext
将按点拆分。只有前端目录包含一个init .py 文件。
推荐阅读
- node.js - MAC ANSI X9.19 是否有任何 nodejs 实现
- api - fetch API 在 Microsoft Edge 和 Firefox 中有效,但被 Failed to load resource: net::ERR_FAILED in Chrome 阻止
- grafana - 将 influxdb 1.8 字符串字段操作到 URL
- python - 在 anaconda 中,如何使用最新的 python 版本创建新环境?
- c# - 我不明白“我”是如何变成 4 的
- python - 收到错误消息:python 中的“CalledProcessError:Command”?
- android - ImageView.setImageDrawable(android.graphics.drawable.Drawable)' 在空对象引用上
- node.js - 当我们中止节点 js 中的 Azure Blob 时无法删除文件
- javascript - 由于跨源,无法从 localhost url 加载图像
- nosql - Oracle NoSQL 数据库云服务的服务控制台是否支持 ORDER BY?