Answers for "ctypes dictionary"

0

ctypes dictionary

# STEP 1: C++ CODE BELOW: 

extern "C" {
    int add(int a, int b) {
      return a + b;
    }
}

# STEP 2: COMPILE

g++ -c -fPIC foo.cpp -o foo.o
g++ -shared -Wl -o libfoo.so  foo.o


# STEP 3: PYTHON CODE: 
from ctypes import cdll, POINTER, c_int
lib = cdll.LoadLibrary('./libfoo.so')

def call_add(a, b):
    assert isinstance(a, int) and isinstance(b, int)

    lib.add.restype = c_int
    lib.add.argtypes = [c_int,c_int]
    return lib.add(c_int(a), c_int(b))

assert call_add(1,4) == 5
Posted by: Guest on September-04-2020

Code answers related to "ctypes dictionary"

Python Answers by Framework

Browse Popular Code Answers by Language