ctypes dictionary

Solutions on MaxInterview for ctypes dictionary by the best coders in the world

showing results for - "ctypes dictionary"
Killian
19 Jul 2019
1# STEP 1: C++ CODE BELOW: 
2
3extern "C" {
4    int add(int a, int b) {
5      return a + b;
6    }
7}
8
9# STEP 2: COMPILE
10
11g++ -c -fPIC foo.cpp -o foo.o
12g++ -shared -Wl -o libfoo.so  foo.o
13
14
15# STEP 3: PYTHON CODE: 
16from ctypes import cdll, POINTER, c_int
17lib = cdll.LoadLibrary('./libfoo.so')
18
19def call_add(a, b):
20    assert isinstance(a, int) and isinstance(b, int)
21
22    lib.add.restype = c_int
23    lib.add.argtypes = [c_int,c_int]
24    return lib.add(c_int(a), c_int(b))
25
26assert call_add(1,4) == 5