Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run .so files using through python script

I have a c program(.c file). I am converting that to a shared object(.so). How can i call and run the shared object from my python code? If possible, please suggest me a list of libraries that can help me to do this task.

like image 842
user3415910 Avatar asked Dec 11 '22 07:12

user3415910


1 Answers

If you want to call functions inside a shared object, the the standard module ctypes is what you are after. No need for any external libraries.

Load a library:

from ctypes import *
# either
libc = cdll.LoadLibrary("libc.so.6")
# or
libc = CDLL("libc.so.6")

Then call a function from the library, the same as calling a Python function:

print(libc.time(None))
like image 143
Alan Avatar answered Dec 22 '22 13:12

Alan