在 C 中扩展嵌入式 Python - 设计与 C++ 交互实例

发布于 2024-11-06 03:38:09 字数 559 浏览 5 评论 0原文

有几个软件包可以帮助自动化编写 C\C++ 和其他语言之间的绑定任务。

就我而言,我想绑定Python,此类包的一些选项是:SWIGBoost.Python罗宾

看起来直接的过程是使用这些包来创建 C\C++ 可链接库(主要是静态函数)并使用它们来扩展高级语言。

然而,我的情况是,我已经有一个用C++开发的工作系统,因此计划将Python嵌入其中,以便将来的开发将用Python进行。

我不清楚如何(如果可能的话)使用这些包来帮助扩展嵌入式 Python,以便 Python 代码能够与系统中已运行的各种 Singleton 实例进行交互,并实例化 C++上课并与他们互动。

我正在寻找的是关于最适合这种情况的设计的见解。

There are several packages out there that help in automating the task of writing bindings between C\C++ and other languages.

In my case, I'd like to bind Python, some options for such packages are: SWIG, Boost.Python and Robin.

It seems that the straight forward process is to use these packages to create C\C++ linkable libraries (with mostly static functions) and have the higher language be extended using them.

However, my situation is that I already have a developed working system in C++ therefore plan to embed Python into it so that future development will be in Python.

It's not clear to me how, and if at all possible, to use these packages in helping to extend embedded Python in such a way that the Python code would be able to interact with the various Singleton instances already running in the system, and instantiate C++ classes and interact with them.

What I'm looking for is an insight regarding the design best fitted for this situation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦初醒的夏天 2024-11-13 03:38:09

Boost.python 可以让你开箱即用地做很多这样的事情,特别是当你使用智能指针时。您甚至可以从 Python 中的 C++ 类继承,然后将这些实例传递回您的 C++ 代码,并且一切仍然有效。关于如何做各种事情我最喜欢的资源是这个(特别是查看“如何”部分):http://wiki.python.org/moin/boost.python/

如果您使用智能指针或侵入式指针,Boost.python 特别好,因为它们会透明地转换为 PyObject 引用计数。此外,它非常擅长使工厂函数看起来像 Python 构造函数,这使得 Python API 非常干净。

如果您不使用智能指针,仍然可以做所有您想做的事情,但是您必须处理各种返回和生命周期策略,这可能会让您头疼。

Boost.python lets you do a lot of those things right out of the box, especially if you use smart pointers. You can even inherit from C++ classes in Python, then pass instances of those back to your C++ code and have everything still work. My favorite resource on how to do various stuff is this (especially check out the "How To" section): http://wiki.python.org/moin/boost.python/ .

Boost.python is especially good if you're using smart pointers or intrusive pointers, as those translate transparently into PyObject reference counting. Also, it's very good at making factory functions look like Python constructors, which makes for very clean Python APIs.

If you're not using smart pointers, it's still possible to do all the things you want, but you have to mess with various return and lifetime policies, which can give you a headache.

南渊 2024-11-13 03:38:09

简而言之:有现代的替代方案 pybind11。

长版本:我还必须嵌入 python。 C++ Python 接口很小,所以我决定使用 C Api。事实证明那是一场噩梦。公开类可以让您编写大量复杂的样板代码。 Boost::Python 通过使用可读的接口定义极大地避免了这种情况。然而我发现 boost 缺乏复杂的文档,并且有些事情你仍然需要调用 Python api。此外,他们的构建系统似乎给人们带来了麻烦。我无法判断,因为我使用系统提供的软件包。最后我尝试了boost python fork pybind11,不得不说它真的很方便,并且修复了boost的一些缺点,比如需要使用Python Api、能够使用lambdas、缺乏易于理解的文档和自动异常翻译。此外,它只是标头,不会对部署产生巨大的提升依赖性,因此我可以明确推荐它。

To make it short: There is the modern alternative pybind11.

Long version: I also had to embed python. The C++ Python interface is small so I decided to use the C Api. That turned out to be a nightmare. Exposing classes lets you write tons of complicated boilerplate code. Boost::Python greatly avoids this by using readable interface definitions. However I found that boost lacks a sophisticated documentation and dor some things you still have to call the Python api. Further their build system seems to give people troubles. I cant tell since i use packages provided by the system. Finally I tried the boost python fork pybind11 and have to say that it is really convenient and fixes some shortcomings of boost like the necessity of the use of the Python Api, ability to use lambdas, the lack of an easy comprehensible documentation and automatic exception translation. Further it is header only and does not pull the huge boost dependency on deployment, so I can definitively recommend it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文