Exposing a C++ API to Python(将 C++ API 暴露给 Python)
问题描述
我目前正在处理一个项目,我必须用 Python 包装 C++ 类才能编写程序脚本.所以我的具体经验还涉及在我们的程序中嵌入 Python 解释器.
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.
我尝试的替代方案是:
Boost.Python
Boost.Python
我喜欢 Boost.Python 生成的更简洁的 API,但它需要用户安装额外的依赖项这一事实使我们转向了 SWIG.
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 对我们的主要优势是它不需要最终用户安装它来使用最终程序.
SWIG's main advantage for us was that it doesn't require end users to install it to use the final program.
您过去常常这样做,您对此有何经验?
What have you used to do this, and what has been your experience with it?
推荐答案
我都使用过(用于同一个项目):Boost 与 STL 更好地集成,尤其是 C++ 异常.此外,它的内存管理机制(试图桥接 C++ 内存管理和 Python GC)比 SWIG 更灵活.但是,SWIG 具有更好的文档,没有外部依赖项,如果您将库封装在 SWIG for Python 中,那么您就已经获得了 Java/Perl/Ruby 包装器的一半以上.
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 much better 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.Python,对于较大的长期项目,对 SWIG 的额外投资是值得的.
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.
这篇关于将 C++ API 暴露给 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 C++ API 暴露给 Python
基础教程推荐
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- CString 到 char* 2021-01-01
