做者:Jerry Jho
连接:https://www.zhihu.com/question/23003213/answer/56121859
来源:知乎
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。html
- C/C++ 调用 Python (基础篇)— 仅讨论Python官方提供的实现方式
- Python 调用 C/C++ (基础篇)— 仅讨论Python官方提供的实现方式
- C/C++ 调用 Python (高级篇)— 使用 Cython
- Python 调用 C/C++ (高级篇)— 使用 SWIG
1 C/C++ 调用 Python(基础篇)
Python 自己就是一个C库。你所看到的可执行体python只不过是个stub。真正的python实体在动态连接库里实现,在Windows平台上,这个文件位于 %SystemRoot%\System32\python27.dll。
你也能够在本身的程序中调用Python,看起来很是容易:
Python 自己就是一个C库。你所看到的可执行体python只不过是个stub。真正的python实体在动态连接库里实现,在Windows平台上,这个文件位于 %SystemRoot%\System32\python27.dll。
你也能够在本身的程序中调用Python,看起来很是容易:
//my_python.c #include <Python.h> int main(int argc, char *argv[]) { Py_SetProgramName(argv[0]); Py_Initialize(); PyRun_SimpleString("print 'Hello Python!'\n"); Py_Finalize(); return 0; }
在Windows平台下,打开Visual Studio命令提示符,编译命令为
cl my_python.c -IC:\Python27\include C:\Python27\libs\python27.lib
在Linux下编译命令为
gcc my_python.c -o my_python -I/usr/include/python2.7/ -lpython2.7
产生可执行文件后,直接运行,结果为输出
Hello Python!
Python库函数 PyRun_SimpleString能够执行字符串形式的Python代码。
虽然很是简单,但这段代码除了能用C语言动态生成一些Python代码以外,并无什么用处。咱们须要的是C语言的数据结构可以和Python交互。
下面举个例子,好比说,有一天咱们用Python写了一个功能特别强大的函数:
def great_function(a): return a + 1
int great_function_from_python(int a) { int res; // some magic return res; }
接下来就要用C来调用Python了,完整的代码以下:
#include <Python.h> int great_function_from_python(int a) { int res; PyObject *pModule,*pFunc; PyObject *pArgs, *pValue; /* import */ pModule = PyImport_Import(PyString_FromString("great_module")); /* great_module.great_function */ pFunc = PyObject_GetAttrString(pModule, "great_function"); /* build args */ pArgs = PyTuple_New(1); PyTuple_SetItem(pArgs,0, PyInt_FromLong(a)); /* call */ pValue = PyObject_CallObject(pFunc, pArgs); res = PyInt_AsLong(pValue); return res; }
从上述代码能够窥见Python内部运行的方式:
- 全部Python元素,module、function、tuple、string等等,实际上都是PyObject。C语言里操纵它们,一概使用PyObject *。
- Python的类型与C语言类型能够相互转换。Python类型XXX转换为C语言类型YYY要使用PyXXX_AsYYY函数;C类型YYY转换为Python类型XXX要使用PyXXX_FromYYY函数。
- 也能够建立Python类型的变量,使用PyXXX_New能够建立类型为XXX的变量。
- 若a是Tuple,则a[i] = b对应于 PyTuple_SetItem(a,i,b),有理由相信还有一个函数PyTuple_GetItem完成取得某一项的值。
- 不只Python语言很优雅,Python的库函数API也很是优雅。
如今咱们获得了一个C语言的函数了,能够写一个main测试它node
#include <Python.h> int great_function_from_python(int a); int main(int argc, char *argv[]) { Py_Initialize(); printf("%d",great_function_from_python(2)); Py_Finalize(); }
编译的方式就用本节开头使用的方法。python
在Linux/Mac OSX运行此示例以前,可能先须要设置环境变量:c++
bash:编程
export PYTHONPATH=.:$PYTHONPATH
csh:api
setenv PYTHONPATH .:$PYTHONPATH
2 Python 调用 C/C++(基础篇)
这种作法称为Python扩展。
好比说,咱们有一个功能强大的C函数:
int great_function(int a) { return a + 1; }
>>> from great_module import great_function >>> great_function(2) 3
#include <Python.h> int great_function(int a) { return a + 1; } static PyObject * _great_function(PyObject *self, PyObject *args) { int _a; int res; if (!PyArg_ParseTuple(args, "i", &_a)) return NULL; res = great_function(_a); return PyLong_FromLong(res); } static PyMethodDef GreateModuleMethods[] = { { "great_function", _great_function, METH_VARARGS, "" }, {NULL, NULL, 0, NULL} }; PyMODINIT_FUNC initgreat_module(void) { (void) Py_InitModule("great_module", GreateModuleMethods); }
除了功能强大的函数great_function外,这个文件中还有如下部分:
- 包裹函数_great_function。它负责将Python的参数转化为C的参数(PyArg_ParseTuple),调用实际的great_function,并处理great_function的返回值,最终返回给Python环境。
- 导出表GreateModuleMethods。它负责告诉Python这个模块里有哪些函数能够被Python调用。导出表的名字能够随便起,每一项有4个参数:第一个参数是提供给Python环境的函数名称,第二个参数是_great_function,即包裹函数。第三个参数的含义是参数变长,第四个参数是一个说明性的字符串。导出表老是以{NULL, NULL, 0, NULL}结束。
- 导出函数initgreat_module。这个的名字不是任取的,是你的module名称添加前缀init。导出函数中将模块名称与导出表进行链接。
在Windows下面,在Visual Studio命令提示符下编译这个文件的命令是bash
cl /LD great_module.c /o great_module.pyd -IC:\Python27\include C:\Python27\libs\python27.lib
/LD 即生成动态连接库。编译成功后在当前目录能够获得 great_module.pyd(其实是dll)。这个pyd能够在Python环境下直接看成module使用。数据结构
在Linux下面,则用gcc编译:app
gcc -fPIC -shared great_module.c -o great_module.so -I/usr/include/python2.7/ -lpython2.7
在当前目录下获得great_module.so,同理能够在Python中直接使用。函数
本部分参考资料
- 《Python源码剖析-深度探索动态语言核心技术》是系统介绍CPython实现以及运行原理的优秀教程。
- Python 官方文档的这一章详细介绍了C/C++与Python的双向互动Extending and Embedding the Python Interpreter
- 关于编译环境,本文所述方法仅为出示原理所用。规范的方式以下:3. Building C and C++ Extensions with distutils
- 做为字典使用的官方参考文档 Python/C API Reference Manual
用以上的方法实现C/C++与Python的混合编程,须要对Python的内部实现有至关的了解。接下来介绍当前较为成熟的技术Cython和SWIG。
3 C/C++ 调用 Python(使用Cython)
在前面的小节中谈到,Python的数据类型和C的数据类型貌似是有某种“一一对应”的关系的,此外,因为Python(确切的说是CPython)自己是由C语言实现的,故Python数据类型之间的函数运算也必然与C语言有对应关系。那么,有没有可能“自动”的作替换,把Python代码直接变成C代码呢?答案是确定的,这就是Cython主要解决的问题。
安装Cython很是简单。Python 2.7.9以上的版本已经自带easy_install:
easy_install -U cython
在Windows环境下依然须要Visual Studio,因为安装的过程须要编译Cython的源代码,故上述命令须要在Visual Studio命令提示符下完成。一下子使用Cython的时候,也须要在Visual Studio命令提示符下进行操做,这一点和第一部分的要求是同样的。
继续以例子说明:
#great_module.pyx cdef public great_function(a,index): return a[index]
这其中有非Python关键字cdef和public。这些关键字属于Cython。因为咱们须要在C语言中使用“编译好的Python代码”,因此得让great_function从外面变得可见,方法就是以“public”修饰。而cdef相似于Python的def,只有使用cdef才可使用Cython的关键字public。
这个函数中其余的部分与正常的Python代码是同样的。
接下来编译 great_module.pyx
cython great_module.pyx
获得great_module.h和great_module.c。打开great_module.h能够找到这样一句声明:
__PYX_EXTERN_C DL_IMPORT(PyObject) *great_function(PyObject *, PyObject *)
//main.c #include <Python.h> #include "great_module.h" int main(int argc, char *argv[]) { PyObject *tuple; Py_Initialize(); initgreat_module(); printf("%s\n",PyString_AsString( great_function( PyString_FromString("hello"), PyInt_FromLong(1) ) )); tuple = Py_BuildValue("(iis)", 1, 2, "three"); printf("%d\n",PyInt_AsLong( great_function( tuple, PyInt_FromLong(1) ) )); printf("%s\n",PyString_AsString( great_function( tuple, PyInt_FromLong(2) ) )); Py_Finalize(); }
编译命令和第一部分相同:
在Windows下编译命令为
cl main.c great_module.c -IC:\Python27\include C:\Python27\libs\python27.lib
gcc main.c great_module.c -o main -I/usr/include/python2.7/ -lpython2.7
#great_module.pyx cdef public char great_function(const char * a,int index): return a[index]
__PYX_EXTERN_C DL_IMPORT(char) great_function(char const *, int);
这样的话,咱们的main函数已经几乎看不到Python的痕迹了:
//main.c #include <Python.h> #include "great_module.h" int main(int argc, char *argv[]) { Py_Initialize(); initgreat_module(); printf("%c",great_function("Hello",2)); Py_Finalize(); }
仍是利用刚才的great_module.pyx,准备一个dllmain.c:
#include <Python.h> #include <Windows.h> #include "great_module.h" extern __declspec(dllexport) int __stdcall _great_function(const char * a, int b) { return great_function(a,b); } BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpReserved) { switch( fdwReason ) { case DLL_PROCESS_ATTACH: Py_Initialize(); initgreat_module(); break; case DLL_PROCESS_DETACH: Py_Finalize(); break; } return TRUE; }
在Visual Studio命令提示符下编译:
cl /LD dllmain.c great_module.c -IC:\Python27\include C:\Python27\libs\python27.lib
会获得一个dllmain.dll。咱们在Excel里面使用它,没错,传说中的Excel与Python混合编程:

参考资料:Cython的官方文档,质量很是高:
Welcome to Cython’s Documentation
4 Python调用C/C++(使用SWIG)
用C/C++对脚本语言的功能扩展是很是常见的事情,Python也不例外。除了SWIG,市面上还有若干用于Python扩展的工具包,比较知名的还有Boost.Python、SIP等,此外,Cython因为能够直接集成C/C++代码,并方便的生成Python模块,故也能够完成扩展Python的任务。
答主在这里选用SWIG的一个重要缘由是,它不只能够用于Python,也能够用于其余语言。现在SWIG已经支持C/C++的好基友Java,主流脚本语言Python、Perl、Ruby、PHP、JavaScript、tcl、Lua,还有Go、C#,以及R。SWIG是基于配置的,也就是说,原则上一套配置改变不一样的编译方法就能适用各类语言(固然,这是理想状况了……)
SWIG的安装方便,有Windows的预编译包,解压即用,绿色健康。主流Linux一般集成swig的包,也能够下载源代码本身编译,SWIG很是小巧,一般安装不会出什么问题。
用SWIG扩展Python,你须要有一个待扩展的C/C++库。这个库有多是你本身写的,也有多是某个项目提供的。这里举一个不浮夸的例子:但愿在Python中用到SSE4指令集的CRC32指令。
首先打开指令集的文档: https://software.intel.com/en-us/node/514245
能够看到有6个函数。分析6个函数的原型,其参数和返回值都是简单的整数。因而书写SWIG的配置文件(为了简化起见,未包含2个64位函数):
/* File: mymodule.i */ %module mymodule %{ #include "nmmintrin.h" %} int _mm_popcnt_u32(unsigned int v); unsigned int _mm_crc32_u8 (unsigned int crc, unsigned char v); unsigned int _mm_crc32_u16(unsigned int crc, unsigned short v); unsigned int _mm_crc32_u32(unsigned int crc, unsigned int v);
swig -python mymodule.i
Windows:
cl /LD mymodule_wrap.c /o _mymodule.pyd -IC:\Python27\include C:\Python27\libs\python27.lib
Linux:
gcc -fPIC -shared mymodule_wrap.c -o _mymodule.so -I/usr/include/python2.7/ -lpython2.7
如今能够当即在Python下使用这个module了:
>>> import mymodule >>> mymodule._mm_popcnt_u32(10) 2
回顾这个配置文件分为3个部分:
Windows下编译:
- 定义module名称mymodule,一般,module名称要和文件名保持一致。
- %{ %} 包裹的部分是C语言的代码,这段代码会原封不动的复制到mymodule_wrap.c
- 欲导出的函数签名列表。直接从头文件里复制过来便可。
还记得本文第2节的那个great_function吗?有了SWIG,事情就会变得如此简单:
/* great_module.i */ %module great_module %{ int great_function(int a) { return a + 1; } %} int great_function(int a);
换句话说,SWIG自动完成了诸如Python类型转换、module初始化、导出代码表生成的诸多工做。
对于C++,SWIG也能够应对。例如如下代码有C++类的定义:
//great_class.h #ifndef GREAT_CLASS #define GREAT_CLASS class Great { private: int s; public: void setWall (int _s) {s = _s;}; int getWall () {return s;}; }; #endif // GREAT_CLASS
对应的SWIG配置文件
/* great_class.i */ %module great_class %{ #include "great_class.h" %} %include "great_class.h"
这里再也不从新敲一遍class的定义了,直接使用SWIG的%include指令
SWIG编译时要加-c++这个选项,生成的扩展名为cxx
swig -c++ -python great_class.i
cl /LD great_class_wrap.cxx /o _great_class.pyd -IC:\Python27\include C:\Python27\libs\python27.lib
Linux,使用C++的编译器
g++ -fPIC -shared great_class_wrap.cxx -o _great_class.so -I/usr/include/python2.7/ -lpython2.7
>>> import great_class >>> c = great_class.Great() >>> c.setWall(5) >>> c.getWall() 5
也就是说C++的class会直接映射到Python class
SWIG很是强大,对于Python接口而言,简单类型,甚至指针,都无需人工干涉便可自动转换,而复杂类型,尤为是自定义类型,SWIG提供了typemap供转换。而一旦使用了typemap,配置文件将再也不在各个语言当中通用。
参考资料:
SWIG的官方文档,质量比较高。 SWIG Users Manual
有个对应的中文版官网,不少年没有更新了。
写在最后: 因为CPython自身的结构设计合理,使得Python的C/C++扩展很是容易。若是打算快速完成任务,Cython(C/C++调用Python)和SWIG(Python调用C/C++)是很不错的选择。可是,一旦涉及到比较复杂的转换任务,不管是继续使用Cython仍是SWIG,仍然须要学习Python源代码。 本文使用的开发环境: Python 2.7.10 Cython 0.22 SWIG 3.0.6 Windows 10 x64 RTM CentOS 7.1 AMD 64 Mac OSX 10.10.4 文中所述原理与具体环境适用性强。 文章所述代码均用于演示,缺少必备的异常检查