4
我在OS X上安裝了包含Homebrew的Gtk + 3包。動態庫的加載順序是否重要?
brew install gtk+3
我可以使用模塊在Python中加載安裝的庫。
$ 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 10aa>
>>> cdll.LoadLibrary('/usr/local/lib/libglib-2.0.0.dylib')
<CDLL '/usr/local/lib/libglib-2.0.0.dylib', handle 7fbd10f0ffb0 at 10aa22dd0>
>>> ^D
到目前爲止好。有一件事困擾着我,如果我試圖加載 上面的兩個相同的庫,但以不同的順序,它會拋出一個符號找不到異常。
$ 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
所以,裝載atk
,然後再glib
,工作。另一種方式不是。誰能解釋這種行爲?
是的,C庫是依賴於順序的,依賴項在順序中比依賴項晚。在C語言中(比如說)libs A B和C,如果某個用在A中的符號在C中被找到,那麼它會被B中的某個相似名稱的interloper所掩蓋。並將它們排序爲B C A中斷。在這裏,你有後一種情況,其中libatk是A,符號是_g_free,而glib *肯定*需要在_g_free的定義後加載。鏈接器跟蹤A使用_g_free需要後來的庫滿足。 –