简体   繁体   English

Python + setuptools:使用boost.python绑定分发预编译的共享库

[英]Python + setuptools: distributing a pre-compiled shared library with boost.python bindings

I have a C++ library (we'll call it Example in the following) for which I wrote Python bindings using the boost.python library. 我有一个C ++库(我们将其称为下面的示例),我使用boost.python库编写了Python绑定。 This Python-wrapped library will be called pyExample. 这个Python包装的库将被称为pyExample。 The entire project is built using CMake and the resulting Python-wrapped library is a file named libpyExample.so . 整个项目使用CMake构建,生成的Python包装库是一个名为libpyExample.so的文件。

When I use the Python bindings from a Python script located in the same directory as libpyExample.so , I simply have to write: 当我使用与libpyExample.so位于同一目录中的Python脚本的Python绑定时,我只需编写:

import libpyExample
libpyExample.hello_world()

and this executes a hello_world() function exposed by the wrapping process. 并执行包装过程中公开的hello_world()函数。

What I want to do 我想做的事

For convenience, I would like my pyExample library to be available from anywhere simply using the command 为方便起见,我希望我的pyExample库可以在任何地方使用该命令

import pyExample

I also want pyExample to be easily installable in any virtualenv in just one command. 我还希望pyExample可以在一个命令中轻松安装在任何virtualenv中。 So I thought a convenient process would be to use setuptools to make that happen. 所以我认为一个方便的过程就是使用setuptools来实现这一目标。 That would therefore imply: 因此,这意味着:

  • Making libpyExample.so visible for any Python script 使libpyExample.so对任何Python脚本libpyExample.so可见
  • Changing the name under which the module is accessed 更改访问模块的名称

I did find many things about compiling C++ extensions with setuptools, but nothing about packaging a pre-compiled C++ extension. 我确实找到了许多关于使用setuptools编译C ++扩展的东西,但没有关于打包预编译的C ++扩展。 Is what I want to do even possible? 我甚至想做什么?

What I do not want to do 我不想做什么

I don't want to build the pyExample library with setuptools, I would like to avoid modifying the existing project too much. 我不想用setuptools构建pyExample库,我想避免过多地修改现有项目。 The CMake build is just fine, I can retrieve the libpyExample.so file very easily. CMake构建很好,我可以很容易地检索libpyExample.so文件。

If I understand your question correctly, you have the following situation: 如果我正确理解您的问题,您会遇到以下情况:

  • you have an existing CMake-based build of a C++ library with Python bindings 你有一个基于CMake的基于Python绑定的C ++库构建
  • you want to package this library with setuptools 你想用setuptools打包这个库

The latter then allows you to call python setup.py install --user , which installs the lib in the site-packages directory and makes it available from every path in your system. 然后,后者允许您调用python setup.py install --user ,它将lib安装在site-packages目录中,并使其可以从系统中的每个路径使用。

What you want is possible, if you overload the classes that setuptools uses to build extensions, so that those classes actually call your CMake build system. 如果你重载setuptools用来构建扩展的类,那么你想要的是什么是可能的,这样这些类实际上会调用你的CMake构建系统。 This is not trivial, but you can find a working example here, provided by the pybind11 project: 这不是一件容易的事,但你可以在这里找到一个工作的例子,由pybind11项目提供:

https://github.com/pybind/cmake_example https://github.com/pybind/cmake_example

Have a look into setup.py , you will see how the classes build_ext and Extension are inherited from and modified to execute the CMake build. 看一下setup.py ,您将看到如何继承和修改类build_extExtension以执行CMake构建。

This should work out of the box for you or with little modification - if your build requires special -D flags to be set. 这应该可以为您开箱即用,或者只需稍加修改即可 - 如果您的构建需要设置特殊的-D标志。 I hope this helps! 我希望这有帮助!

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

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