尝试在Python中加载C DLL时出现错误的ELF类

时间:2019-06-28 22:21:50

标签: python c python-2.7 ctypes

C文件:data.c

    int aa[3] = {10, 20, 30};

    int* my_func()
    {
      return aa;
    }

创建.so
>> gcc -c -Wall -Werror -fpic data.c
>> gcc -shared -o libdata.so data.o


python文件:data.py

    import ctypes
    f = ctypes.CDLL('./libdata.so').my_func
    f.restype = ctypes.POINTER(ctypes.c_int)
    print [i for i in f().contents]

要执行的命令:
>> python data.py

错误消息:

Traceback (most recent call last):
  File "data.py", line 11, in <module>
    f = ctypes.CDLL('./libdata.so').my_func
  File "/app/vbuild/RHEL6-x86_64/python/2.7.9/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./libdata.so: wrong ELF class: ELFCLASS32

1 个答案:

答案 0 :(得分:0)

您需要指定数组中的项目数,其工作原理如下:

$ cat data.py
import ctypes
f = ctypes.CDLL('./libdata.so').my_func
f.restype = ctypes.POINTER(ctypes.c_int*3)
print([x for x in f().contents])

$ python data.py 
[10, 20, 30]