python how to load multiple C libraries

chenchao at inhand.com.cn chenchao at inhand.com.cn
Thu Sep 17 09:41:26 CEST 2015


hi:
    I use ctypes load multiple C libraries. Code like this:
    .....
    history = CDLL("/usr/lib/libhistory.so", mode=RTLD_GLOBAL|RTLD_LAZY)
    ncurses = CDLL("/usr/lib/libncurses.so", mode=RTLD_GLOBAL|RTLD_LAZY)
    readline = CDLL("/usr/lib/libreadline.so", mode=RTLD_GLOBAL|RTLD_LAZY)
    ...
    When I execute my python scripts, it come out followed error:
    /var/app/python/scripts # python test.py
     Traceback (most recent call last):
      File "test.py", line 4, in <module>
     history = CDLL("/usr/lib/libhistory.so", mode=RTLD_GLOBAL|RTLD_LAZY)
     NameError: name 'RTLD_LAZY' is not defined.

   So, I checked my ctypes source code, I found RTLD_LAZY defined in 
dlfcn.h header file like this:
/* The MODE argument to `dlopen' contains one of the following: */
#define RTLD_LAZY 0x00001 /* Lazy function call binding.  */
#define RTLD_NOW  0x00002 /* Immediate function call binding.  */
#define RTLD_BINDING_MASK   0x3 /* Mask of binding time value.  */
#define RTLD_NOLOAD 0x00004 /* Do not load the object.  */
#if 0 /* uClibc doesnt support these */
#define RTLD_DEEPBIND 0x00008 /* Use deep binding.  */
#endif

/* If the following bit is set in the MODE argument to `dlopen',
    the symbols of the loaded object and its dependencies are made
    visible as if the object were linked directly into the program.  */
#define RTLD_GLOBAL 0x00100

/* Unix98 demands the following flag which is the inverse to RTLD_GLOBAL.
    The implementation does this by default and so we can define the
    value to zero.  */
#define RTLD_LOCAL  0

/* Do not delete object when closed.  */
#define RTLD_NODELETE 0x01000
The first of all, I have added followed codes in function:
PyMODINIT_FUNC init_ctypes(void) {...} in _ctypes.c file.

     /* add RTLD_LAZY and RTLD_NOW definitions*/
     PyModule_AddObject(m, "RTLD_LAZY", PyInt_FromLong(RTLD_LAZY));
     PyModule_AddObject(m, "RTLD_NOW", PyInt_FromLong(RTLD_NOW));

So, Is there anybody know how the CDLL can accept mode flag:RTLD_LAZY?

On 09/16/2015 09:13 PM, Laura Creighton wrote:
> In a message of Wed, 16 Sep 2015 17:35:18 +0800, "chenchao at inhand.com.cn" write
> s:
>> hi:
>> I encountered a problem. I use ctypes load multiple C  libraries, but
>> the libraries have dependence each other.So, how can i load these
>> libraries. For example, I have two libraries:A、B, but A depends on B, B
>> depends on A. So how do I load them? Is there anybody know how to do it?
> I don't know how to do this with ctypes (which doesn't mean it cannot be
> done, just that I haven't ever wanted to do this). What happens if you
> make a small file that loads both separately and loads that one first?
>
> It may be that you can get what you want with by loading the first lib
> RTLD_LAZY instead of RTLD_NOW.  see:
> http://stackoverflow.com/questions/22591472/dyanamic-library-linking-by-rtld-lazy
> see: this very old thread, maybe CTypes knows about it now.
> http://osdir.com/ml/python.ctypes/2006-10/msg00029.html
>
> But I have never tried.
>
> These days I used cffi instead of ctypes.
>
> If you use cffi https://pypi.python.org/pypi/cffi
> there should not be any problem if you use ffi.set_source(),
> only once, but mentioning both libs in the libraries=[...] argument.
>
> Laura
>
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150917/75415374/attachment.html>


More information about the Python-list mailing list