Nick
Nick

Reputation: 2959

Python ctypes.cdll.LoadLibrary works differently between 2.6.5 and 2.7.1

I've got a system with both Python version 2.6.5 and 2.7.1 and I'm noticing that one LoadLibrary works, the other does not.

Python 2.7.1 (r271:86832, Nov 30 2010, 10:03:07)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary("./mylib.so")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary
    return self._dlltype(name)
  File "/usr/local/lib/python2.7/ctypes/__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./mylib.so: undefined symbol: compress2

and in working 2.6.5 land:

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import cdll
>>> cdll.LoadLibrary("./mylib.so")
<CDLL './mylib.so', handle 98bbd88 at b785c94c>

Does anyone know a good way to figure out why python 2.7.1 is not working?

Upvotes: 2

Views: 4284

Answers (1)

Nick
Nick

Reputation: 2959

For some reason, Python ctypes gets super anal about library dependencies. I still have no figured out why 2.6.5 was working above, but doing the following fixes it:

When compiling mylib.so, I needed to specify or link to the zlib library explicity. Since "compress2" is from the zlib library, I can do it by the following:

g++ blah blah mylib.so blah blah -lz

The -lz links to the zlib library and it will then show up if you do a "ldd mylib.so" This fixes it and allows "LoadLibrary" to not fail.

Upvotes: 1

Related Questions