我可以使用 C++ 扩展 Python 时的功能?
Python 手册说你可以用 C 和 C++ 创建 Python 模块。 使用 C++ 时可以利用类和模板之类的东西吗? 它不会造成与其他库和解释器的不兼容吗?
The Python manual says that you can create modules for Python in both C and C++. Can you take advantage of things like classes and templates when using C++? Wouldn't it create incompatibilities with the rest of the libraries and with the interpreter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
钩子函数的实现是用 C 还是 C++ 实现并不重要。 事实上,我已经看到一些积极使用 C++ 模板甚至 Boost 库的 Python 扩展。 没问题。:-)
It doesn't matter whether your implementation of the hook functions is implemented in C or in C++. In fact, I've already seen some Python extensions which make active use of C++ templates and even the Boost library. No problem. :-)
boost 人员有一种很好的自动化方法来包装 C++ 代码以供 python 使用。
它被称为:Boost.Python
它比 SWIG 更好地处理 C++ 的一些结构,特别是模板元编程。
The boost folks have a nice automated way to do the wrapping of C++ code for use by python.
It is called: Boost.Python
It deals with some of the constructs of C++ better than SWIG, particularly template metaprogramming.
您感兴趣的是一个名为 SWIG 的程序。 它将生成 C++ 代码的 Python 包装器和接口。 我将它与模板、继承、命名空间等一起使用,效果很好。
What you're interested in is a program called SWIG. It will generate Python wrappers and interfaces for C++ code. I use it with templates, inheritance, namespaces, etc. and it works well.
您应该能够使用 C++ 语言的所有功能。 扩展 Python 文档 (2.6.2) 说您可以使用 C++,但提到以下注意事项:
第一个限制“不能使用具有构造函数的全局或静态对象”,与大多数 C++ 编译器初始化具有此类存储持续时间的对象的方式有关。 例如,考虑以下代码:
编译器必须发出特殊代码,以便在执行 main 之前为“f”调用“Foo”构造函数。 如果您的 Python 扩展中有具有静态存储持续时间的对象,并且 Python 解释器未针对 C++ 进行编译和链接,则不会创建此特殊的初始化代码。
第二个限制“Python 解释器将调用的函数(特别是模块初始化函数)必须使用 extern "C" 进行声明”,与 C++ 名称修饰有关。 大多数 C++ 编译器都会修改它们的名称,以便它们可以使用为 C 工具链提供的相同链接器。 例如,假设您有:
C++ 编译器可能将对名称“a_function_python_calls”的引用转换为“a_function_python_calls@1vga”之类的内容。 在这种情况下,当您尝试链接 Python 库时,可能会遇到无法解析的外部问题。
You should be able to use all of the features of the C++ language. The Extending Python Documentation (2.6.2) says that you may use C++, but mentions the followings caveats:
The first restriction, "global or static objects with constructors cannot be used", has to do with the way most C++ compiler initialize objects with this type of storage duration. For example, consider the following code:
The compiler has to emit special code so that the 'Foo' constructor gets invoked for 'f' before main gets executed. If you have objects with static storage duration in your Python extension and the Python interpreter is not compiled and linked for C++, then this special initialization code will not be created.
The second restriction, "Functions that will be called by the Python interpreter (in particular, module initialization functions) have to be declared using extern "C"", has to do with C++ name mangling. Most C++ compilers mangle their names so that they can use the same linkers provided for C toolchains. For example say you had:
the C++ compiler may convert references to the name 'a_function_python_calls' to something like 'a_function_python_calls@1vga'. In which case you may get an unresolved external when trying to link with the Python library.