Search code examples
pythoncmoduleextending

Loading DLL's in Python


I'm following the guide found here:

http://www.dalkescientific.com/writings/NBN/c_extensions.html

for creating C extensions to python. But when I try to run any python program after building that module, such as the first one listed or mandelbrot.py (listed towards the end of the page). I get the error on the line libc = ctypes.CDLL("libc.dylib", ctypes.RTLD_GLOBAL)

The error is:

Traceback (most recent call last):
File "cos.py", line 5, in < module >
libc = ctypes.CDLL("libc.dylib", ctypes.RTLD_GLOBAL)
File "/usr/lib/python2.7/ctypes/__init__.py", line 365, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libc.dylib: cannot open shared object file: No such file or directory

I'm using Linux so I'm not sure if that is the problem. And if it is, how would I accomplish this on Linux? I know dll is how Windows refers to shared objects. But does the syntax change? I can't seem to find an answer anywhere.


Solution

  • On GNU/Linux the cos() function is located in a library called libm.so. So you need to replace "libc.dylib" with "libm.so".