Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
main.py 1.05 KiB
# https://solarianprogrammer.com/2019/07/18/python-using-c-cpp-libraries-ctypes/
# https://gist.github.com/elyezer/7099291

# https://www.geeksforgeeks.org/turning-a-function-pointer-to-callable/
# https://stackoverflow.com/questions/9420673/is-it-possible-to-compile-c-code-using-python
# https://documentation.help/Python-3.2.1/ctypes.html

import ctypes
import os
import subprocess 
  
def buildLibrary(): 
  
    # store the return code of the c program(return 0) 
    # and display the output 
    s = subprocess.check_call("bash compile.sh", shell=True) 
    print("return code:\n{}".format(s)) 

buildLibrary()


libmean = ctypes.CDLL("libmean.so.1") # loads the shared library
libmean.mean.restype = ctypes.c_double # define mean function return type
print(libmean.mean)
print(libmean.mean(ctypes.c_double(10), ctypes.c_double(3))) # call mean function needs cast arg types
libmean.mean.argtypes = [ctypes.c_double, ctypes.c_double] # define arguments types
print(libmean.mean(10, 3)) # call mean function does not need cast arg types
print(type(libmean.mean(10, 5)))