Mac OS X Lion Python Ctype CDLL Error lib.so.6: Image Not Found

I am new to Python. When I tried the following Python code example type library on Mac OS X Lion:

#hello.py from ctypes import * cdll.LoadLibrary("libc.so.6") libc = CDLL("libc.so.6") message_string = "Hello World! Hello Python!\n" libc.printf("Testing :%s",message_string) // 

An error has occurred:

 Traceback (most recent call last): File "cprintf.py", line 2, in <module> cdll.LoadLibrary("libc.so.6") File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 431, in LoadLibrary return self._dlltype(name) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 353, in __init__ self._handle = _dlopen(self._name, mode) OSError: dlopen(libc.so.6, 6): image not found 

Can someone tell me what is the matter? BTW, I tried this on Windows and Linux; he worked well. You need to create some configuration for ctype.

Many thanks,

Ricky

+7
source share
3 answers

Mac OS X .dylib libraries have a .dylib extension instead of a .so . In this case, /usr/lib/libc.dylib is what you want to load load libc.dylib .

+14
source

OS X uses ".dylib" to extend its shared objects, so you need to use "libc.dylib".

+4
source

A cross-platform solution would have to use something like this:

 import platform import ctypes libc = ctypes.cdll.LoadLibrary("libc.{}".format("so.6" if platform.uname()[0] != "Darwin" else "dylib")) # or ctypes.CDLL("libc.{}".format("so.6" if platform.uname()[0] != "Darwin" else "dylib")) 

Not quite sure what the difference is between these alternatives, since both seem to work well!

0
source

All Articles