python how to load multiple C libraries

chenchao at chenchao at
Thu Sep 17 09:41:26 CEST 2015

    I use ctypes load multiple C libraries. Code like this:
    history = CDLL("/usr/lib/", mode=RTLD_GLOBAL|RTLD_LAZY)
    ncurses = CDLL("/usr/lib/", mode=RTLD_GLOBAL|RTLD_LAZY)
    readline = CDLL("/usr/lib/", mode=RTLD_GLOBAL|RTLD_LAZY)
    When I execute my python scripts, it come out followed error:
    /var/app/python/scripts # python
     Traceback (most recent call last):
      File "", line 4, in <module>
     history = CDLL("/usr/lib/", 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.  */

/* 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" 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:
> see: this very old thread, maybe CTypes knows about it now.
> But I have never tried.
> These days I used cffi instead of ctypes.
> If you use 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: <>

More information about the Python-list mailing list