C ++ - 用Python ctypes的结合 - 在函数返回多个值 [英] C++ - Python Binding with ctypes - Return multiple values in function
问题描述
我发现C ++的Python结合这个例子:
调用C / C ++的蟒蛇?
根据该答案,我创建了一些testfiles:
I found this example of C++ Python binding: Calling C/C++ from python? According to the answer there, I created some testfiles:
Foo.cpp中:
#include <iostream>
#include <utility>
int bar_2(int a, int b){
return a*b;
}
std::pair<int, int> divide(int dividend, int divisor)
{
return std::make_pair(dividend / divisor, dividend % divisor);
}
extern "C" {
int bar_2_py(int a, int b){ return bar_2(a,b); }
std::pair<int, int> divide_py(int d, int div){return divide(d,div);}
}
fooWrapper.py:
fooWrapper.py:
#!/usr/bin/env python
from ctypes import cdll
lib = cdll.LoadLibrary('./libfoo.so')
def bar_2(a, b):
res = lib.bar_2_py( a,b )
return res
def divide(d,div):
res = lib.divide_py(d,div)
return res
然后
g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so foo.o
创建libfoo.so
creates libfoo.so
如果我将其导入并运行功能IPython中,我得到了功能bar_2正确的价值,但是一个(部分)错误答案的鸿沟:
If I import it and run the functions in iPython I get the right value for function "bar_2" but a (partly) wrong answer for "divide":
from fooWrapper import bar_2, divide
bar_2(10,2) # returns 20, which is right
divide(10,3) # returns 3
显然,返回值是正确的对中的第一值(如10/3 INT分割为3)。但第二个值迷路。
Obviously, the return-value is right for the first value of the pair (as 10/3 int division is 3). But the second value was getting lost.
那么,这将是获得多个值的最佳做法(在这种情况下两个整数值)?
So, what would be best practice to obtain multiple values (in this case 2 integer values)?
感谢您!
推荐答案
我不认为 ctypes的
允许一个翻译的std ::对
来没有太多的样板code一个Python元组。特别是因为的std ::对
是的一个特点C ++ 11
标准和 ctypes的
仅工作ç
式的功能。
I don't think ctypes
allows one to translate std::pair
to a python tuple without much boilerplate code. Especially since std::pair
is a feature of the c++11
standard, and ctypes
only works with c
-style functions [citation / verification needed].
我建议将使用的输出参数的,在 C
办法返回多个值。道理很简单,在 C
- 函数返回它以指针的价值观, example.c
:
I suggest would using output parameters, the c
way to return multiple values. The idea is simple, the c
-function returns it's values by pointer, example.c
:
void divide_modulo(int a, int b, int *div, int *rest)
{
*div = a / b;
*rest = a % b;
}
然后,它编译成一个共享库:
Then compile it to a shared library:
gcc -o libexample.so -shared example.c
的 libexample.so
现在允许你通过在指针写信给蟒蛇整数ç
这是作为传球这样的参数:
The libexample.so
now allows you to write to python integers via a pointer in c
which are passes as parameters like this:
import ctypes
lib = ctypes.cdll.LoadLibrary('./libexample.so')
def divide_modulo(a, b):
div = ctypes.c_int(0)
rest = ctypes.c_int(0)
lib.divide_modulo(a, b, ctypes.byref(div), ctypes.byref(rest))
return (div.value, rest.value)
print(divide_modulo(11, 4))
在调用时, ctypes.byref
包装 lib.divide_modulo
与 DIV
和其他
转换的 INT
来一个指向 INT
。
The ctypes.byref
wrapper when calling lib.divide_modulo
with div
and rest
converts an int
to a pointer to an int
.
这篇关于C ++ - 用Python ctypes的结合 - 在函数返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!