
我使用python的ctypes调用so库,然后其他普通的指针就可以调用,但是这种类似于python字典请问应该怎么调用,最好贴个代码

我使用python的ctypes调用so库,然后其他普通的指针就可以调用,但是这种类似于python字典请问应该怎么调用,最好贴个代码
你得先将std::map转换为普通的指针,在传递给so库
#include <map>
#include <stdint.h>
extern "C" {
void* map2ptr(std::map<int32_t, int32_t>* map) {
return static_cast<void*>(map);
}
}
在py加载执行
import ctypes
from ctypes import *
lib = cdll.LoadLibrary("./libtest.so")
class IntMap(Structure):
_fields_ = [("key", c_int32),
("value", c_int32)]
lib.map2ptr.restype = c_void_p
lib.map2ptr.argtypes = [POINTER(c_void_p)]
py_dict = {1: 10, 2: 20, 3: 30}
cpp_map = [IntMap(k, v) for k, v in py_dict.items()]
map_ptr = lib.map2ptr(cpp_map)