Search This Blog

Labels

Friday, December 17, 2010

《扩展和嵌入Python解释器(译稿)》1.11 Writing Extensions in

1.11 Writing Extensions in C++ 用 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++编译器链接的话就没有这个问题。由Python解释器调用的函数(特别是模块初始化函数)必须用“extern "C"”来声明。没的必要将Python头文件包围进extern "C" {…}——如果变量‘__cplusplus’被定义了那么就已经使用了这种形式。(所有当前的C++编译器都定义了这个变量)。

No comments:

Post a Comment