我可以在扩展Python时使用C ++功能吗?

时间:2022-11-14 23:17:23

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?

Python手册说您可以在C和C ++中为Python创建模块。使用C ++时,你能利用类和模板之类的东西吗?它不会与其他库和解释器产生不兼容性吗?

4 个解决方案

#1


8  

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. :-)

是否在C或C ++中实现了钩子函数的实现并不重要。事实上,我已经看到一些Python扩展,它们主动使用C ++模板甚至是Boost库。没问题。 :-)

#2


3  

The boost folks have a nice automated way to do the wrapping of C++ code for use by python.

增强人员有一个很好的自动方式来包装C ++代码以供python使用。

It is called: Boost.Python

它被称为:Boost.Python

It deals with some of the constructs of C++ better than SWIG, particularly template metaprogramming.

它比SWIG更好地处理C ++的一些构造,特别是模板元编程。

#3


2  

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.

您感兴趣的是一个名为SWIG的程序。它将为C ++代码生成Python包装器和接口。我将它与模板,继承,命名空间等一起使用,效果很好。

#4


1  

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:

您应该能够使用C ++语言的所有功能。扩展Python文档(2.6.2)说您可以使用C ++,但提到以下警告:

It is possible to write extension modules in C++. Some restrictions apply. If the main program (the Python interpreter) is compiled and linked by the C compiler, global or static objects with constructors cannot be used. This is not a problem if the main program is linked by the C++ compiler. Functions that will be called by the Python interpreter (in particular, module initialization functions) have to be declared using extern "C". It is unnecessary to enclose the Python header files in extern "C" {...} — they use this form already if the symbol __cplusplus is defined (all recent C++ compilers define this symbol).

可以用C ++编写扩展模块。一些限制适用。如果主程序(Python解释器)由C编译器编译和链接,则不能使用具有构造函数的全局或静态对象。如果主程序由C ++编译器链接,这不是问题。必须使用extern“C”声明Python解释器调用的函数(特别是模块初始化函数)。没有必要将Python头文件括在extern“C”{...}中 - 如果定义了符号__cplusplus,它们就已经使用了这个形式(所有最近的C ++编译器都定义了这个符号)。

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:

第一个限制是“不能使用构造函数的全局或静态对象”,这与大多数C ++编译器使用这种类型的存储持续时间初始化对象的方式有关。例如,请考虑以下代码:

class Foo { Foo() { } };

static Foo f;

int main(int argc, char** argv) {}

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.

编译器必须发出特殊代码,以便在执行main之前为'f'调用'Foo'构造函数。如果在Python扩展中有对象具有静态存储持续时间,并且没有为C ++编译和链接Python解释器,则不会创建此特殊初始化代码。

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:

第二个限制,“将由Python解释器调用的函数(特别是模块初始化函数)必须使用extern”C“”声明,与C ++名称修改有关。大多数C ++编译器都会破坏它们的名称,以便它们可以使用为C工具链提供的相同链接器。比如说你有:

void a_function_python_calls(void* foo);

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.

C ++编译器可能会将名称'a_function_python_calls'的引用转换为类似'a_function_python_calls @ 1vga'的引用。在这种情况下,在尝试链接Python库时,您可能会得到一个未解析的外部。

#1


8  

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. :-)

是否在C或C ++中实现了钩子函数的实现并不重要。事实上,我已经看到一些Python扩展,它们主动使用C ++模板甚至是Boost库。没问题。 :-)

#2


3  

The boost folks have a nice automated way to do the wrapping of C++ code for use by python.

增强人员有一个很好的自动方式来包装C ++代码以供python使用。

It is called: Boost.Python

它被称为:Boost.Python

It deals with some of the constructs of C++ better than SWIG, particularly template metaprogramming.

它比SWIG更好地处理C ++的一些构造,特别是模板元编程。

#3


2  

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.

您感兴趣的是一个名为SWIG的程序。它将为C ++代码生成Python包装器和接口。我将它与模板,继承,命名空间等一起使用,效果很好。

#4


1  

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:

您应该能够使用C ++语言的所有功能。扩展Python文档(2.6.2)说您可以使用C ++,但提到以下警告:

It is possible to write extension modules in C++. Some restrictions apply. If the main program (the Python interpreter) is compiled and linked by the C compiler, global or static objects with constructors cannot be used. This is not a problem if the main program is linked by the C++ compiler. Functions that will be called by the Python interpreter (in particular, module initialization functions) have to be declared using extern "C". It is unnecessary to enclose the Python header files in extern "C" {...} — they use this form already if the symbol __cplusplus is defined (all recent C++ compilers define this symbol).

可以用C ++编写扩展模块。一些限制适用。如果主程序(Python解释器)由C编译器编译和链接,则不能使用具有构造函数的全局或静态对象。如果主程序由C ++编译器链接,这不是问题。必须使用extern“C”声明Python解释器调用的函数(特别是模块初始化函数)。没有必要将Python头文件括在extern“C”{...}中 - 如果定义了符号__cplusplus,它们就已经使用了这个形式(所有最近的C ++编译器都定义了这个符号)。

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:

第一个限制是“不能使用构造函数的全局或静态对象”,这与大多数C ++编译器使用这种类型的存储持续时间初始化对象的方式有关。例如,请考虑以下代码:

class Foo { Foo() { } };

static Foo f;

int main(int argc, char** argv) {}

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.

编译器必须发出特殊代码,以便在执行main之前为'f'调用'Foo'构造函数。如果在Python扩展中有对象具有静态存储持续时间,并且没有为C ++编译和链接Python解释器,则不会创建此特殊初始化代码。

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:

第二个限制,“将由Python解释器调用的函数(特别是模块初始化函数)必须使用extern”C“”声明,与C ++名称修改有关。大多数C ++编译器都会破坏它们的名称,以便它们可以使用为C工具链提供的相同链接器。比如说你有:

void a_function_python_calls(void* foo);

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.

C ++编译器可能会将名称'a_function_python_calls'的引用转换为类似'a_function_python_calls @ 1vga'的引用。在这种情况下,在尝试链接Python库时,您可能会得到一个未解析的外部。