来自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
On 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'对象不可调用”错误?
$ 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
答案 0 :(得分:4)
您将小写cdll
(LibraryLoader
)与大写CDLL
混淆,后者是共享库的构造函数。
此代码将按预期工作:
libc = ctypes.CDLL("/lib/x86_64-linux-gnu/libc.so.6")