小编T_W*_*T_W的帖子

C++ - Python 绑定与 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 = …
Run Code Online (Sandbox Code Playgroud)

c++ python binding ctypes

2
推荐指数
1
解决办法
1861
查看次数

标签 统计

binding ×1

c++ ×1

ctypes ×1

python ×1