Is the order in which dynamic libraries are loaded?

I am on OS X and installed the Gtk + 3 package with Homebrew.

brew install gtk+3

I can load installed libraries in Python using a module ctypes.

$ python2.6
Python 2.6.9 (unknown, Oct 23 2015, 19:19:20)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary('/usr/local/lib/libatk-1.0.0.dylib')
<CDLL '/usr/local/lib/libatk-1.0.0.dylib', handle 7fbd10f1a250 at 10aa33210>
>>> cdll.LoadLibrary('/usr/local/lib/libglib-2.0.0.dylib')
<CDLL '/usr/local/lib/libglib-2.0.0.dylib', handle 7fbd10f0ffb0 at 10aa22dd0>
>>> ^D

So far so good. My concern is that if I try to load the same two libraries above, but in a different order, it throws a Symbol not found exception .

$ python2.6
Python 2.6.9 (unknown, Oct 23 2015, 19:19:20)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary('/usr/local/lib/libglib-2.0.0.dylib')
<CDLL '/usr/local/lib/libglib-2.0.0.dylib', handle 7fad13d00d60 at 10a688210>
>>> cdll.LoadLibrary('/usr/local/lib/libatk-1.0.0.dylib')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.py", line 423, in LoadLibrary
    return self._dlltype(name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/ctypes/__init__.py", line 345, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(/usr/local/lib/libatk-1.0.0.dylib, 6): Symbol not found: _g_free
  Referenced from: /usr/local/lib/libatk-1.0.0.dylib
  Expected in: flat namespace
 in /usr/local/lib/libatk-1.0.0.dylib
>>> ^D

So, it loads first atk, and then glib. There is no other way. Can anyone explain this behavior?

+4
source share
1 answer

C. main.c, foo() foo.so, bar() bar.so, ( fun) g_free g_malloc glib.

$ ls -Flas main.o libfoo.so libbar.so | cut -c3-13,34-37,51-
-rwxrwxr-x 2976 libbar.so*    # has bar(), which calls g_free/g_malloc
-rwxrwxr-x 3504 libfoo.so*    # has foo(), which calls bar()   
-rw-rw-r-- 2864 main.o        # has main(), which calls foo()

:

$ gcc -o main main.o -L. -lfoo -lbar -lglib-2.0
$ LD_LIBRARY_PATH=. ./main
worked!

:

$ gcc -o main main.o -L. -lbar -lfoo -lglib-2.0
./libfoo.so: undefined reference to `bar'
collect2: error: ld returned 1 exit status

, . "man ld" :

, undefined , . undefined , , .

cdll.

+1

All Articles