【发布时间】:2018-06-11 13:29:50
【问题描述】:
来自https://en.wikipedia.org/wiki/Foreign_function_interface
ctypes 模块可以从共享库/DLL 加载 C 函数 即时并在之间自动转换简单的数据类型 Python和C语义如下:
import ctypes libc = ctypes.CDLL( '/lib/libc.so.6' ) # under Linux/Unix t = libc.time(None) # equivalent C code: t = time(NULL) print t
在 Lubuntu 18.04 上
$ whereis libc
libc: /usr/lib/x86_64-linux-gnu/libc.a /usr/lib/x86_64-linux-gnu/libc.so /usr/share/man/man7/libc.7.gz
$ locate libc.so
/lib/i386-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libc.so.6
/usr/lib/x86_64-linux-gnu/libc.so
$ ls -l /usr/lib/x86_64-linux-gnu/libc.so
-rw-r--r-- 1 root root 298 Apr 16 16:14 /usr/lib/x86_64-linux-gnu/libc.so
我想知道为什么加载 libc 共享库会出现“'LibraryLoader' object is not callable”错误?
$ python3 --version
Python 3.6.5
$ python3
>>> import ctypes
>>> libc=ctypes.cdll("/usr/lib/x86_64-linux-gnu/libc.so")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'LibraryLoader' object is not callable
>>> libc=ctypes.cdll("/lib/x86_64-linux-gnu/libc.so.6")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'LibraryLoader' object is not callable
>>> libc=ctypes.cdll("/lib/i386-linux-gnu/libc.so.6")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'LibraryLoader' object is not callable
【问题讨论】:
标签: python linux ctypes libc dynamic-loading