C++ - Python 绑定与 ctypes - 在函数中返回多个值

T_W*_*T_W 2 c++ python binding ctypes

我发现了这个 C++ Python 绑定示例: Calling C/C++ from python? 根据那里的答案,我创建了一些测试文件:

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);}
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

然后

g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl,-soname,libfoo.so -o libfoo.so  foo.o
Run Code Online (Sandbox Code Playgroud)

创建 libfoo.so

如果我导入它并在 iPython 中运行这些函数,我会得到函数“bar_2”的正确值,但是“divide”的(部分)错误答案:

from fooWrapper import bar_2, divide
bar_2(10,2) # returns 20, which is right
divide(10,3) # returns 3
Run Code Online (Sandbox Code Playgroud)

显然,返回值适合该对的第一个值(因为 10/3 int 除法是 3)。但是第二个值正在丢失。

那么,获得多个值(在本例中为 2 个整数值)的最佳实践是什么?

谢谢!

Her*_*ert 7

我认为没有太多样板代码就不允许ctypes转换std::pair为 python 元组。特别是因为它std::pairc++11标准的一个特性,并且ctypes只适用于c-style 函数 [需要引用/验证]。

我建议使用输出参数c返回多个值的方式。这个想法很简单,c- 函数通过指针返回它的值,example.c

void divide_modulo(int a, int b, int *div, int *rest)
{
    *div  = a / b;
    *rest = a % b;
}
Run Code Online (Sandbox Code Playgroud)

然后将其编译为共享库:

gcc -o libexample.so -shared example.c
Run Code Online (Sandbox Code Playgroud)

libexample.so现在允许你通过指针写入蟒蛇整数c这是因为经过这样的参数:

void divide_modulo(int a, int b, int *div, int *rest)
{
    *div  = a / b;
    *rest = a % b;
}
Run Code Online (Sandbox Code Playgroud)

ctypes.byref调用包装时lib.divide_modulo使用divrest转换的int到的指针int