Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

python how to load multiple C libraries

859 views
Skip to first unread message

chen...@inhand.com.cn

unread,
Sep 16, 2015, 5:35:43 AM9/16/15
to pytho...@python.org
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?


Chris Angelico

unread,
Sep 16, 2015, 5:41:27 AM9/16/15
to pytho...@python.org
How are the libraries built? A circular dependency is always a
problem. How would a C program handle this?

ChrisA

chen...@inhand.com.cn

unread,
Sep 16, 2015, 6:27:21 AM9/16/15
to pytho...@python.org
hi:
Most current compilers and linkers will search all object files,
regard-less of order, but since not all compilers do this it is best to
follow the
convention of ordering object files from left to right . Python do that.
So, is there anybody know how to solve the follow problem?

Laura Creighton

unread,
Sep 16, 2015, 9:14:43 AM9/16/15
to chen...@inhand.com.cn, pytho...@python.org, l...@openend.se
In a message of Wed, 16 Sep 2015 17:35:18 +0800, "chen...@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



chen...@inhand.com.cn

unread,
Sep 17, 2015, 3:43:40 AM9/17/15
to pytho...@python.org, Laura Creighton
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?

Laura Creighton

unread,
Sep 17, 2015, 5:42:44 AM9/17/15
to chen...@inhand.com.cn, pytho...@python.org, Laura Creighton
In a message of Thu, 17 Sep 2015 15:41:26 +0800, "chen...@inhand.com.cn" write
s:
It's a known open bug.
https://bugs.python.org/issue20276

Some people who have really needed to get this to work with ctypes have
modified the source for ctypes as discribed here:
http://osdir.com/ml/python.ctypes/2006-10/msg00029.html

I just stopped using ctypes and use cffi instead.
https://cffi.readthedocs.org/en/latest/

Laura

0 new messages