pythonshared-librariesctypes

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.


Solution

  • 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))