How to use librt function in Python?

c, linux, perforce, python, shared-libraries

Solution

You need to load it with

 ctypes.CDLL('librt.so', mode=ctypes.RTLD_GLOBAL)

in order to make it available to other libraries.

Problem

I'm trying to use a third-party .so, P4API.so, that calls clock_gettime defined in librt.so and would like users of my script not to have to set LD_PRELOAD. So in the init.py file, I have: ``` import ctypes librt = ctypes.cdll.LoadLibrary('librt.so') ``` This loads the library fine, but running the script still emits: ``` ImportError: /path/to/P4API.so: undefined symbol: clock_gettime ``` I've tried: ``` __builtins__['clock_gettime'] = librt.clock_gettime ``` but that doesn't work, either. How can I get P4API.so to recognize the loaded librt?

Original source