我应该如何将 Python 嵌入到 C++ 中? Builder/Delphi 2010 应用程序?
我有兴趣尝试在我的应用程序中嵌入 Python,让用户在应用程序环境中运行 Python 脚本、访问内部(C++ 实现的)对象等。我对此很陌生,所以不知道到底是什么我正在做。
我已阅读在另一个应用程序中嵌入Python,尽管这似乎只谈论 C API 和平面 C 函数,而不是类或对象(除非我错过了一些东西),并且它的“在 C++ 中嵌入 Python”部分只有两句话长。但是,我还遇到了如何使用 boost::python< /strong> 这看起来很棒。
有一个问题:C++ Builder 2010 不支持 boost::python C++ Builder 2010 。
因此,鉴于此,在使用 C++ Builder 2010 编译的 C++ 应用程序中嵌入 Python 的最佳方法是什么,以及使用哪种最佳技术,如何公开/集成类和对象以使 Python 编码器能够访问程序面向对象的内部结构? 我是否错过了标准方法?将内部类或实例化对象作为对象暴露给 Python 是否容易,或者 API 是否真正是 C 风格或平面/非 OO,如果是这样,通过此类 API 模拟底层 OO 层的最佳方法是什么?
注意:我实际上使用 RAD Studio,其中包括 C++ Builder 和 Delphi。可能可以使用某种特定于 Delphi 的绑定,但我遇到的绑定是 六七岁,或者是新手 (Python 2.6),但似乎没有任何文档,并且问题列表中有评论,例如 “有人读过这些[原文如此]评论吗?有人参与这个项目吗?” 这并不令人鼓舞。但请随意包含 Delphi 特定的答案,特别是如果您认为它们可能在组合的 D+CB 应用程序中工作。我很欣赏所有的答案,即使它们不是很完美 - 我可以研究,我只需要指导去哪里。不过,本机 C++ 解决方案可能是理想的,因为使用 VCL 派生对象有其自身的局限性。
感谢您的投入!
I'm interested in experimenting with embedding Python in my application, to let the user run Python scripts within the application environment, accessing internal (C++-implemented) objects, etc. I'm quite new to this so don't know exactly what I'm doing.
I have read Embedding Python in Another Application, though this seems to talk only about a C API and flat C functions, not classes or objects (unless I've missed something) and its "Embedding Python in C++" section is only two sentences long. However, I also came across how to use boost::python and this looks excellent.
There's one problem: boost::python is not supported by C++ Builder 2010.
So, given this, what is the best approach for embedding Python in a C++ application compiled with C++ Builder 2010, and, using whichever technique is best, how do you expose / integrate classes and objects to give the Python coder access to the object-oriented internals of a program? Have I missed a standard approach? Is exposing internal classes or instantiated objects to Python as objects easy, or is the API truly C-style or flat / non-OO, and if so what's the best approach to mimic an underlying OO layer through such an API?
Note: I actually use RAD Studio, which includes both C++ Builder and Delphi. It may be possible to make use of some sort of Delphi-specific binding, but the ones I've encountered are six or seven years old, or are new-ish (Python 2.6) but don't seem to have any documentation and have comments in the issue list like "Anyone reads thiese [sic] comments anyway? Anyone working on this project?" which is not encouraging. But please feel free to include Delphi-specific answers especially if you think it's likely they'll work in a combined D+CB app. I appreciate all answers even if they aren't quite perfect - I can research, I just need pointers on where to go. A native C++ solution would probably be ideal, though, since using VCL-derived objects has its own limitations.
Thanks for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不应该害怕 google groups 中的 P4D 项目。它看起来不活跃,部分原因是它已经非常稳定且功能齐全。这些组件用于更活跃的 PyScripter 应用程序,该应用程序是最好的 Python 开发编辑器之一目前可用。 PyScripter 用 Delphi 编写并使用 P4D 组件。因此,它还提供了一个关于如何使用 P4D 组件的非常全面的示例,尽管 P4D 源检查提供的示例已经足以入门。
You should not be afraid of the P4D project at google groups. It seems inactive because, in part, it is very stable and full-featured already. Those components are used in the much more active PyScripter application which is one of the best python development editors currently available. PyScripter is written in Delphi and uses the P4D components. As such, it also presents a very comprehensive example of how to use the P4D components, although the examples provided with the P4D source checkout are already good enough to get started.
你自己已经回答了。这句话的后半部分是正确的。
编译后,C++ 中并不存在对象和类,只有少数结构(vtable),以及解释一些 OO 数据的其他结构,前提是 RTTI 已激活。这就是为什么不可能使用类和对象来弥合 Python 和 C++ 之间的差距。
您当然可以自己构建它,创建一组 C 函数和一些数据结构,然后创建一个 OO 层。但你不能开箱即用地这样做。
例如,类 Car:
被翻译为:
对 getDoors 的调用:
被翻译为:
You have already answered yourself. The latter part of the sentence is correct.
Objects and classes do not exist in C++ as soon as you compile, only a few structures (vtables), and also another ones explaining some OO data, provided that RTTI is activated. That's why it is not possible to bridge the gap between Python and C++ using classes and objects.
You can build that surely by yourself, creating a set of C functions along with some data structures, and then an OO-layer. But you cannot do that out of the box.
For instance, class Car:
Is translated to:
And a call to getDoors:
Is translated as:
您可以使用 swig 生成 C++ 到 $SCRIPTLANG 包装器。
You can generate C++ to $SCRIPTLANG wrappers with swig.