为什么加载 libc 共享库会出现“'LibraryLoader' object is not callable”错误?

Why does loading the libc shared library have "'LibraryLoader' object is not callable" error?

来自https://en.wikipedia.org/wiki/Foreign_function_interface

the ctypes module can load C functions from shared libraries/DLLs on-the-fly and translate simple data types automatically between Python and C semantics as follows:

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' 对象不可调用”错误?

$ 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

另见 https://unix.stackexchange.com/questions/449107/what-differences-and-relations-are-between-the-various-libc-so

你混淆了小写 cdll(这是一个 LibraryLoader) with upper case CDLL,它是共享库的构造函数。

此代码将按预期工作:

libc = ctypes.CDLL("/lib/x86_64-linux-gnu/libc.so.6")