将 C++ API 暴露给 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/276761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exposing a C++ API to Python
提问by Marcos Lara
I'm currently working on a project were I had to wrap the C++ classes with Python to be able to script the program. So my specific experience also involved embedding the Python interpreter in our program.
我目前正在处理一个项目,我必须用 Python 包装 C++ 类才能编写程序脚本。所以我的具体经验还涉及在我们的程序中嵌入 Python 解释器。
The alternatives I tried were:
我尝试的替代方案是:
Boost.Python
I liked the cleaner API produced by Boost.Python, but the fact that it would have required that users install an additional dependency made us switch to SWIG.
SWIG
SWIG's main advantage for us was that it doesn't require end users to install it to use the final program.
Boost.Python
我喜欢 Boost.Python 生成的更简洁的 API,但它需要用户安装额外的依赖项这一事实使我们转向了 SWIG。
斯威格
SWIG 对我们的主要优势是它不需要最终用户安装它来使用最终程序。
What have you used to do this, and what has been your experience with it?
你曾经做过什么,你有什么经验?
采纳答案by Max Maximus
I've used both (for the same project): Boost is better integrated with the STL, and especially C++ exceptions. Also, its memory management mechanism (which tries to bridge C++ memory management and Python GC) is way more flexible than SWIG's. However, SWIG has muchbetter documentation, no external dependencies, and if you get the library wrapped in SWIG for Python you're more than half-way there to getting a Java/Perl/Ruby wrapper as well.
我都使用过(用于同一个项目):Boost 与 STL 更好地集成在一起,尤其是 C++ 异常。此外,它的内存管理机制(试图桥接 C++ 内存管理和 Python GC)比 SWIG 更灵活。但是,SWIG有很多更好的文档,没有外部的依赖关系,如果你包裹在SWIG为Python库你超过中途那里得到一个Java / Perl的/ Ruby的包装也是如此。
I don't think there's a clear-cut choice: for smaller projects, I'd go with Boost.Python again, for larger long-lived projects, the extra investment in SWIG is worth it.
我认为没有明确的选择:对于较小的项目,我会再次使用 Boost.Python,对于较大的长期项目,对 SWIG 的额外投资是值得的。
回答by orip
回答by bhadra
I suggest SIP. SIP is better than SWIG due to the following reasons:
我建议SIP。由于以下原因,SIP 优于 SWIG:
For a given set of files, swig generates more duplicate (overhead) code than SIP. SIP manages to generate less duplicate (overhead) code by using a library file which can be statically or dynamically linked. In other words SIP has better scalability.
Execution time of SIP is much less than that of SWIG. Refer Python Wrapper Tools: A Performance Study. Unfortunately link appears broken. I have a personal copy which can be shared on request.
对于给定的一组文件,swig 生成的重复(开销)代码比 SIP 多。通过使用可以静态或动态链接的库文件,SIP 设法生成更少的重复(开销)代码。换句话说,SIP 具有更好的可扩展性。
SIP 的执行时间远少于 SWIG。请参阅Python 包装器工具:性能研究。不幸的是,链接似乎已损坏。我有一份个人副本,可以应要求共享。
回答by Toni Ru?a
回答by wr.
A big plus for Boost::Python is that it allows for tab completion in the ipython shell: You import a C++ class, exposed by Boost directly, or you subclass it, and from then on, it really behaves like a pure Python class.
Boost::Python 的一大优点是它允许在 ipython shell 中完成制表符:您导入一个 C++ 类,由 Boost 直接公开,或者您对它进行子类化,从那时起,它的行为就像一个纯 Python 类。
The downside: It takes so long to install and use Boost that all the Tab-completion time-saving won't ever amortize ;-(
缺点:安装和使用 Boost 需要很长时间,以至于所有 Tab 完成时间节省都不会摊销;-(
So I prefer Swig: No bells and whistles, but works reliably after a short introductory example.
所以我更喜欢 Swig:没有花里胡哨的东西,但在一个简短的介绍性示例之后可以可靠地工作。