Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X Lion Python Ctype CDLL error lib.so.6 : image not found

I am a beginner in Python. When I tried the following Python sample code with 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 occurred as following:

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 anyone tell me what's the matter? BTW, I tried this on Windows and Linux; it worked well. Need I make some configuration for ctype.

Thanks very much,

Ricky

like image 856
Gickian Avatar asked Jul 19 '12 05:07

Gickian


3 Answers

A cross platform solution would be 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, as both seem to work nicely!

like image 149
holroy Avatar answered Nov 12 '22 05:11

holroy


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

like image 29
Kevin Grant Avatar answered Nov 12 '22 05:11

Kevin Grant


OS X uses ".dylib" for the extension of its shared objects, so you need to use "libc.dylib" instead.

like image 32
Ignacio Vazquez-Abrams Avatar answered Nov 12 '22 03:11

Ignacio Vazquez-Abrams