ctypes:'LibraryLoader'object is not callable

使用 python3 调用 C++ 编译的动态 so 文件

ctypes.cdll("cpptest.so",mode=ctypes.RTLD_GLOBAL)

运行 py 文件后报错为

Traceback (most recent call last):
  File "pytest.py", line 10, in <module>
    ctypes.cdll("cpptest.so",mode=ctypes.RTLD_GLOBAL)
TypeError: 'LibraryLoader' object is not callable

但是使用

mylib = ctypes.cdll.LoadLibrary("cpptest.so")

运行正确

有大神遇到相似问题吗

# -*- coding: utf-8 -*-
import ctypes

#mylib = ctypes.cdll.LoadLibrary("cpptest.so")
ctypes.cdll("cpptest.so",mode=ctypes.RTLD_GLOBAL)
#ctypes.LibraryLoader(u'cpptest.so')
class sub_struct(ctypes.Structure):
    _fields_ = [
        ("test_char_p",ctypes.c_char_p),
        ("test_int",ctypes.c_int),
    ]

class struct_def(ctypes.Structure):
    _fields_ = [
        ("stru_string",ctypes.c_char_p),
        ("stru_int", ctypes.c_int),
        ("stru_arr_num", ctypes.c_char*4),
        ("son_struct", sub_struct)
    ]

struct_mystruct = struct_def()

struct_mystruct.stru_string = b"string in the struct"

struct_mystruct.stru_int = 99

struct_mystruct.stru_arr_num = b"ABCD"

struct_mystruct.son_struct.test_char_p =b"sub struct of the string"

struct_mystruct.son_struct.test_int = 10

mylib.test(struct_mystruct,ctypes.byref(struct_mystruct),struct_mystruct)
print("======40=======")
print(struct_mystruct.stru_int)
C++ c
最佳答案

发现错误,加载 so 文件时不应该使用 ctypes.cdll 方式 应该使用 ctypes.CDLL 方式,因为疏忽导致大小写问题错误

5年前 评论
讨论数量: 1

发现错误,加载 so 文件时不应该使用 ctypes.cdll 方式 应该使用 ctypes.CDLL 方式,因为疏忽导致大小写问题错误

5年前 评论