-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python调用C++程序备忘笔记.html
406 lines (376 loc) · 34.6 KB
/
Python调用C++程序备忘笔记.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="littlewhite" />
<meta name="copyright" content="littlewhite" />
<meta property="og:type" content="article" />
<meta name="twitter:card" content="summary">
<meta name="keywords" content="python, c++, Language, " />
<meta property="og:title" content="Python调用C++程序备忘笔记 "/>
<meta property="og:url" content="https://chukeer.github.io/Python调用C++程序备忘笔记.html" />
<meta property="og:description" content="Python的优点是开发效率高,使用方便,C++则是运行效率高,这两者可以相辅相成,不管是在Python项目中嵌入C++代码,或是在C++项目中用Python实现外围功能,都可能遇到Python调用C++模块的需求,下面列举出集中c++代码导出成Python接口的几种基本方法 原生态导出¶ Python解释器就是用C实现,因此只要我们的C++的数据结构能让Python认识,理论上就是可以被直接调用的。我们实现test1.cpp如下 #include <Python.h> int Add(int x, int y) { return x + y; } int Del(int x, int y) { return x - y; } PyObject* WrappAdd(PyObject* self, PyObject* args) { int x, y ..." />
<meta property="og:site_name" content="楚客" />
<meta property="og:article:author" content="littlewhite" />
<meta property="og:article:published_time" content="2015-09-02T00:00:00+08:00" />
<meta property="" content="2015-09-02T00:00:00+08:00" />
<meta name="twitter:title" content="Python调用C++程序备忘笔记 ">
<meta name="twitter:description" content="Python的优点是开发效率高,使用方便,C++则是运行效率高,这两者可以相辅相成,不管是在Python项目中嵌入C++代码,或是在C++项目中用Python实现外围功能,都可能遇到Python调用C++模块的需求,下面列举出集中c++代码导出成Python接口的几种基本方法 原生态导出¶ Python解释器就是用C实现,因此只要我们的C++的数据结构能让Python认识,理论上就是可以被直接调用的。我们实现test1.cpp如下 #include <Python.h> int Add(int x, int y) { return x + y; } int Del(int x, int y) { return x - y; } PyObject* WrappAdd(PyObject* self, PyObject* args) { int x, y ...">
<title>Python调用C++程序备忘笔记 · 楚客
</title>
<!--
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.1/css/font-awesome.css" rel="stylesheet">
--!>
<link href="https://chukeer.github.io/theme/css/bootstrap-combined.min.css" rel="stylesheet">
<link href="https://chukeer.github.io/theme/css/font-awesome.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/pygments.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/tipuesearch/tipuesearch.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/elegant.css" media="screen">
<link rel="stylesheet" type="text/css" href="https://chukeer.github.io/theme/css/custom.css" media="screen">
</head>
<body>
<div id="content-sans-footer">
<div class="navbar navbar-static-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="https://chukeer.github.io/"><span class=site-name>楚客</span></a>
<div class="nav-collapse collapse">
<ul class="nav pull-right top-menu">
<li ><a href="https://chukeer.github.io">Home</a></li>
<li ><a href="https://chukeer.github.io/categories.html">Categories</a></li>
<li ><a href="https://chukeer.github.io/tags.html">Tags</a></li>
<li ><a href="https://chukeer.github.io/archives.html">Archives</a></li>
<li><form class="navbar-search" action="https://chukeer.github.io/search.html" onsubmit="return validateForm(this.elements['q'].value);"> <input type="text" class="search-query" placeholder="Search" name="q" id="tipue_search_input"></form></li>
</ul>
</div>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span1"></div>
<div class="span10">
<article>
<div class="row-fluid">
<header class="page-header span10 offset2">
<h1><a href="https://chukeer.github.io/Python调用C++程序备忘笔记.html"> Python调用C++程序备忘笔记 </a></h1>
</header>
</div>
<div class="row-fluid">
<div class="span2 table-of-content">
<nav>
<h4>Contents</h4>
<div class="toc">
<ul>
<li><a href="#_1">原生态导出</a></li>
<li><a href="#boost">通过boost实现</a></li>
<li><a href="#_2">导出类</a></li>
<li><a href="#_3">导出变参函数</a></li>
<li><a href="#python">导出带Python对象的接口</a></li>
</ul>
</div>
</nav>
</div>
<div class="span8 article-content">
<p>Python的优点是开发效率高,使用方便,C++则是运行效率高,这两者可以相辅相成,不管是在Python项目中嵌入C++代码,或是在C++项目中用Python实现外围功能,都可能遇到Python调用C++模块的需求,下面列举出集中c++代码导出成Python接口的几种基本方法</p>
<h2 id="_1">原生态导出<a class="headerlink" href="#_1" title="Permanent link">¶</a></h2>
<p>Python解释器就是用C实现,因此只要我们的C++的数据结构能让Python认识,理论上就是可以被直接调用的。我们实现test1.cpp如下</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><Python.h></span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">Add</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">Del</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">PyObject</span><span class="o">*</span> <span class="nf">WrappAdd</span><span class="p">(</span><span class="n">PyObject</span><span class="o">*</span> <span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTuple</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s">"ii"</span><span class="p">,</span> <span class="o">&</span><span class="n">x</span><span class="p">,</span> <span class="o">&</span><span class="n">y</span><span class="p">))</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"i"</span><span class="p">,</span> <span class="n">Add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">));</span>
<span class="p">}</span>
<span class="n">PyObject</span><span class="o">*</span> <span class="nf">WrappDel</span><span class="p">(</span><span class="n">PyObject</span><span class="o">*</span> <span class="n">self</span><span class="p">,</span> <span class="n">PyObject</span><span class="o">*</span> <span class="n">args</span><span class="p">)</span>
<span class="p">{</span>
<span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">PyArg_ParseTuple</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="s">"ii"</span><span class="p">,</span> <span class="o">&</span><span class="n">x</span><span class="p">,</span> <span class="o">&</span><span class="n">y</span><span class="p">))</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nb">NULL</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">Py_BuildValue</span><span class="p">(</span><span class="s">"i"</span><span class="p">,</span> <span class="n">Del</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">));</span>
<span class="p">}</span>
<span class="k">static</span> <span class="n">PyMethodDef</span> <span class="n">test_methods</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span>
<span class="p">{</span><span class="s">"Add"</span><span class="p">,</span> <span class="n">WrappAdd</span><span class="p">,</span> <span class="n">METH_VARARGS</span><span class="p">,</span> <span class="s">"something"</span><span class="p">},</span>
<span class="p">{</span><span class="s">"Del"</span><span class="p">,</span> <span class="n">WrappDel</span><span class="p">,</span> <span class="n">METH_VARARGS</span><span class="p">,</span> <span class="s">"something"</span><span class="p">},</span>
<span class="p">{</span><span class="nb">NULL</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">}</span>
<span class="p">};</span>
<span class="k">extern</span> <span class="s">"C"</span>
<span class="kt">void</span> <span class="n">inittest1</span><span class="p">()</span>
<span class="p">{</span>
<span class="n">Py_InitModule</span><span class="p">(</span><span class="s">"test1"</span><span class="p">,</span> <span class="n">test_methods</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>编译命令如下</p>
<div class="highlight"><pre><span></span>g++ -fPIC -shared test1.cpp -I/usr/include/python2.6 -o test1.so
</pre></div>
<p>运行Python解释器,测试如下</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">test1</span>
<span class="o">>>></span> <span class="n">test1</span><span class="o">.</span><span class="n">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">3</span>
</pre></div>
<p>这里要注意一下几点</p>
<ul>
<li>如果生成的动态库名字为test1,则源文件里必须有inittest1这个函数,且Py_InitModule的第一个参数必须是“test1”,否则Python导入模块会失败</li>
<li>如果是cpp源文件,inittest1函数必须用extern "C"修饰,如果是c源文件,则不需要。原因是Python解释器在导入库时会寻找initxxx这样的函数,而C和C++对函数符号的编码方式不同,C++在对函数符号进行编码时会考虑函数长度和参数类型,具体可以通过<code>nm test1.so</code>查看函数符号,c++filt工具可通过符号反解出函数原型</li>
</ul>
<h2 id="boost">通过boost实现<a class="headerlink" href="#boost" title="Permanent link">¶</a></h2>
<p>我们使用和上面同样的例子,实现test2.cpp如下</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><boost/python/module.hpp></span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf"><boost/python/def.hpp></span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="p">;</span>
<span class="kt">int</span> <span class="nf">Add</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="nf">Del</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">BOOST_PYTHON_MODULE</span><span class="p">(</span><span class="n">test2</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">def</span><span class="p">(</span><span class="s">"Add"</span><span class="p">,</span> <span class="n">Add</span><span class="p">);</span>
<span class="n">def</span><span class="p">(</span><span class="s">"Del"</span><span class="p">,</span> <span class="n">Del</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>其中BOOST_PYTHON_MODULE的参数为要导出的模块名字</p>
<p>编译命令如下</p>
<div class="highlight"><pre><span></span>g++ test2.cpp -fPIC -shared -o test2.so -I/usr/include/python2.6 -I/usr/local/include -L/usr/local/lib -lboost_python
</pre></div>
<p><strong>注意: 编译时需要指定boost头文件和库的路径,我这里分别是/usr/local/include和/usr/local/lib</strong></p>
<p>或者通过setup.py导出模块</p>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5
6
7
8
9</pre></div></td><td class="code"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/env python</span>
<span class="kn">from</span> <span class="nn">distutils.core</span> <span class="kn">import</span> <span class="n">setup</span>
<span class="kn">from</span> <span class="nn">distutils.extension</span> <span class="kn">import</span> <span class="n">Extension</span>
<span class="n">setup</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">"PackageName"</span><span class="p">,</span>
<span class="n">ext_modules</span><span class="o">=</span><span class="p">[</span>
<span class="n">Extension</span><span class="p">(</span><span class="s2">"test2"</span><span class="p">,</span> <span class="p">[</span><span class="s2">"test2.cpp"</span><span class="p">],</span>
<span class="n">libraries</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"boost_python"</span><span class="p">])</span>
<span class="p">])</span>
</pre></div>
</td></tr></table>
<p>Extension的第一个参数为模块名,第二个参数为文件名</p>
<p>执行如下命令</p>
<div class="highlight"><pre><span></span>python setup.py build
</pre></div>
<p>这时会生成build目录,找到里面的test2.so,并进入同一级目录,验证如下</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">test2</span>
<span class="o">>>></span> <span class="n">test2</span><span class="o">.</span><span class="n">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">3</span>
<span class="o">>>></span> <span class="n">test2</span><span class="o">.</span><span class="n">Del</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="o">-</span><span class="mi">1</span>
</pre></div>
<h2 id="_2">导出类<a class="headerlink" href="#_2" title="Permanent link">¶</a></h2>
<p>test3.cpp实现如下</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><boost/python.hpp></span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Test</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="kt">int</span> <span class="n">Add</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="kt">int</span> <span class="n">Del</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="n">BOOST_PYTHON_MODULE</span><span class="p">(</span><span class="n">test3</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">class_</span><span class="o"><</span><span class="n">Test</span><span class="o">></span><span class="p">(</span><span class="s">"Test"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"Add"</span><span class="p">,</span> <span class="o">&</span><span class="n">Test</span><span class="o">::</span><span class="n">Add</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"Del"</span><span class="p">,</span> <span class="o">&</span><span class="n">Test</span><span class="o">::</span><span class="n">Del</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>注意:BOOST_PYTHON_MODULE里的.def使用方法有点类似Python的语法,等同于</p>
<div class="highlight"><pre><span></span><span class="nt">class_</span><span class="o"><</span><span class="nt">Test</span><span class="o">>(</span><span class="s2">"Test"</span><span class="o">)</span><span class="nc">.def</span><span class="o">(</span><span class="s2">"Add"</span><span class="o">,</span> <span class="o">&</span><span class="nt">Test</span><span class="o">:</span><span class="nd">:Add</span><span class="o">);</span>
<span class="nt">class_</span><span class="o"><</span><span class="nt">Test</span><span class="o">>(</span><span class="s2">"Test"</span><span class="o">)</span><span class="nc">.def</span><span class="o">(</span><span class="s2">"Del"</span><span class="o">,</span> <span class="o">&</span><span class="nt">Test</span><span class="o">:</span><span class="nd">:Del</span><span class="o">);</span>
</pre></div>
<p>编译命令如下</p>
<div class="highlight"><pre><span></span>g++ test3.cpp -fPIC -shared -o test3.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
</pre></div>
<p>测试如下</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">test3</span>
<span class="o">>>></span> <span class="n">test</span> <span class="o">=</span> <span class="n">test3</span><span class="o">.</span><span class="n">Test</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">test</span><span class="o">.</span><span class="n">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">3</span>
<span class="o">>>></span> <span class="n">test</span><span class="o">.</span><span class="n">Del</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="o">-</span><span class="mi">1</span>
</pre></div>
<h2 id="_3">导出变参函数<a class="headerlink" href="#_3" title="Permanent link">¶</a></h2>
<p>test4.cpp实现如下</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><boost/python.hpp></span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">Test</span>
<span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
<span class="kt">int</span> <span class="n">Add</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">100</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">+</span> <span class="n">y</span> <span class="o">+</span> <span class="n">z</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">};</span>
<span class="kt">int</span> <span class="nf">Del</span><span class="p">(</span><span class="kt">const</span> <span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">const</span> <span class="kt">int</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">100</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">-</span> <span class="n">y</span> <span class="o">-</span> <span class="n">z</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS</span><span class="p">(</span><span class="n">Add_member_overloads</span><span class="p">,</span> <span class="n">Add</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">BOOST_PYTHON_FUNCTION_OVERLOADS</span><span class="p">(</span><span class="n">Del_overloads</span><span class="p">,</span> <span class="n">Del</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="n">BOOST_PYTHON_MODULE</span><span class="p">(</span><span class="n">test4</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">class_</span><span class="o"><</span><span class="n">Test</span><span class="o">></span><span class="p">(</span><span class="s">"Test"</span><span class="p">)</span>
<span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">"Add"</span><span class="p">,</span> <span class="o">&</span><span class="n">Test</span><span class="o">::</span><span class="n">Add</span><span class="p">,</span> <span class="n">Add_member_overloads</span><span class="p">(</span><span class="n">args</span><span class="p">(</span><span class="s">"x"</span><span class="p">,</span> <span class="s">"y"</span><span class="p">,</span> <span class="s">"z"</span><span class="p">),</span> <span class="s">"something"</span><span class="p">));</span>
<span class="n">def</span><span class="p">(</span><span class="s">"Del"</span><span class="p">,</span> <span class="n">Del</span><span class="p">,</span> <span class="n">Del_overloads</span><span class="p">(</span><span class="n">args</span><span class="p">(</span><span class="s">"x"</span><span class="p">,</span> <span class="s">"y"</span><span class="p">,</span> <span class="s">"z"</span><span class="p">),</span> <span class="s">"something"</span><span class="p">));</span>
<span class="p">}</span>
</pre></div>
<p>这里Add和Del函数均采用了默认参数,Del为普通函数,Add为类成员函数,这里分别调用了不同的宏,宏的最后两个参数分别代表函数的最少参数个数和最多参数个数</p>
<p>编译命令如下</p>
<div class="highlight"><pre><span></span>g++ test4.cpp -fPIC -shared -o test4.so -I/usr/include/python2.6 -I/usr/local/ include/boost -L/usr/local/lib -lboost_python
</pre></div>
<p>测试如下</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">test4</span>
<span class="o">>>></span> <span class="n">test</span> <span class="o">=</span> <span class="n">test4</span><span class="o">.</span><span class="n">Test</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">test</span><span class="o">.</span><span class="n">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="mi">103</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">test</span><span class="o">.</span><span class="n">Add</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="mi">6</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">test4</span><span class="o">.</span><span class="n">Del</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="o">-</span><span class="mi">1</span>
<span class="o">>>></span> <span class="k">print</span> <span class="n">test4</span><span class="o">.</span><span class="n">Del</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="n">z</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="o">-</span><span class="mi">1</span>
</pre></div>
<h2 id="python">导出带Python对象的接口<a class="headerlink" href="#python" title="Permanent link">¶</a></h2>
<p>既然是导出为Python接口,调用者难免会使用Python特有的数据结构,比如tuple,list,dict,由于原生态方法太麻烦,这里只记录boost的使用方法,假设要实现如下的Python函数功能</p>
<div class="highlight"><pre><span></span>def Square(list_a)
{
return [x * x for x in list_a]
}
</pre></div>
<p>即对传入的list每个元素计算平方,返回list类型的结果</p>
<p>代码如下</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf"><boost/python.hpp></span><span class="cp"></span>
<span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="o">::</span><span class="n">list</span> <span class="n">Square</span><span class="p">(</span><span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="o">::</span><span class="n">list</span><span class="o">&</span> <span class="n">data</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">boost</span><span class="o">::</span><span class="n">python</span><span class="o">::</span><span class="n">list</span> <span class="n">ret</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">len</span><span class="p">(</span><span class="n">data</span><span class="p">);</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">ret</span><span class="p">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">data</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span>
<span class="p">}</span>
<span class="k">return</span> <span class="n">ret</span><span class="p">;</span>
<span class="p">}</span>
<span class="n">BOOST_PYTHON_MODULE</span><span class="p">(</span><span class="n">test5</span><span class="p">)</span>
<span class="p">{</span>
<span class="n">def</span><span class="p">(</span><span class="s">"Square"</span><span class="p">,</span> <span class="n">Square</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
<p>编译命令如下</p>
<div class="highlight"><pre><span></span>g++ test5.cpp -fPIC -shared -o test5.so -I/usr/include/python2.6 -I/usr/local/include/boost -L/usr/local/lib -lboost_python
</pre></div>
<p>测试如下</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">test5</span>
<span class="o">>>></span> <span class="n">test5</span><span class="o">.</span><span class="n">Square</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">])</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
</pre></div>
<p>boost实现了boost::python::tuple, boost::python::list, boost::python::dict这几个数据类型,使用方法基本和Python保持一致,具体方法可以查看boost头文件里的boost/python/tuple.hpp及其它对应文件</p>
<p>另外比较常用的一个函数是<code>boost::python::make_tuple()</code>,使用方法如下</p>
<div class="highlight"><pre><span></span><span class="nt">boost</span><span class="o">:</span><span class="nd">:python</span><span class="o">:</span><span class="nd">:tuple</span> <span class="nt">t</span> <span class="o">=</span> <span class="nt">boost</span><span class="o">:</span><span class="nd">:python</span><span class="o">:</span><span class="nd">:make_tuple</span><span class="o">(</span><span class="nt">a</span><span class="o">,</span> <span class="nt">b</span><span class="o">,</span> <span class="nt">c</span><span class="o">);</span>
</pre></div>
<hr/>
<aside>
<nav>
<ul class="articles-timeline">
<li class="previous-article">« <a href="https://chukeer.github.io/Linux程序编译链接动态库版本的问题.html" title="Previous: Linux程序编译链接动态库版本的问题">Linux程序编译链接动态库版本的问题</a></li>
<li class="next-article"><a href="https://chukeer.github.io/gdb调试技巧备忘.html" title="Next: gdb调试技巧备忘">gdb调试技巧备忘</a> »</li>
</ul>
</nav>
</aside>
</div>
<section>
<div class="span2" style="float:right;font-size:0.9em;">
<table class="table">
<!-- <time pubdate="pubdate" datetime="2015-09-02T00:00:00+08:00"> 9 2, 2015</time> -->
<tr>
<td>Published</td>
<td><time pubdate="pubdate" datetime="2015-09-02T00:00:00+08:00">2015- 9-2</time></td>
</tr>
<tr>
<td>Category</td>
<td><a class="category-link" href="https://chukeer.github.io/categories.html#language-ref">Language</a></td>
</tr>
<tr>
<td>Tags</td>
<td>
<ul class="list-of-tags tags-in-article">
<li><a href="https://chukeer.github.io/tags.html#c-ref">c++
<span>15</span>
</a></li>
<li><a href="https://chukeer.github.io/tags.html#python-ref">python
<span>6</span>
</a></li>
</ul>
</td>
</tr>
</table>
</div>
</section>
</div>
</article>
</div>
<div class="span1"></div>
</div>
</div>
<div id="push"></div>
</div>
<footer>
<div id="footer">
<ul class="footer-content">
<li class="elegant-power">Powered by <a href="http://getpelican.com/" title="Pelican Home Page">Pelican</a>. Theme: <a href="http://oncrashreboot.com/pelican-elegant" title="Theme Elegant Home Page">Elegant</a> by <a href="http://oncrashreboot.com" title="Talha Mansoor Home Page">Talha Mansoor</a></li>
</ul>
</div>
</footer> <!--
<script src="http://code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
-->
<script src="https://chukeer.github.io/theme/js/jquery.min.js"></script>
<script src="https://chukeer.github.io/theme/js/bootstrap.min.js"></script>
<script>
function validateForm(query)
{
return (query.length > 0);
}
</script>
<script>
$("div.article-content table").addClass("table table-hover");
</script>
</body>
<!-- Theme: Elegant built for Pelican
License : http://oncrashreboot.com/pelican-elegant -->
</html>