为什么加载libc共享库有“'LibraryLoader'对象不可调用”错误?

时间:2018-06-11 13:29:50

标签: python linux ctypes libc dynamic-loading

来自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

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

1 个答案:

答案 0 :(得分:4)

您将小写cdllLibraryLoader)与大写CDLL混淆,后者是共享库的构造函数。

此代码将按预期工作:

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