V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
checgg
V2EX  ›  问与答

请教 Python 如何调用 arm 编译的 so 文件?

  •  
  •   checgg · 2018-05-13 11:50:08 +08:00 · 2933 次点击
    这是一个创建于 2167 天前的主题,其中的信息可能已经有所发展或是发生改变。

    so 文件信息

    # readelf -h libencrypt.so
    
    ELF Header:
      Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
      Class:                             ELF32
      Data:                              2's complement, little endian
      Version:                           1 (current)
      OS/ABI:                            UNIX - System V
      ABI Version:                       0
      Type:                              DYN (Shared object file)
      Machine:                           ARM
      Version:                           0x1
      Entry point address:               0x0
      Start of program headers:          52 (bytes into file)
      Start of section headers:          16768 (bytes into file)
      Flags:                             0x5000000, Version5 EABI
      Size of this header:               52 (bytes)
      Size of program headers:           32 (bytes)
      Number of program headers:         7
      Size of section headers:           40 (bytes)
      Number of section headers:         21
      Section header string table index: 20
    

    python 及系统版本信息( docker 容器)

    root@arm32v7-openjdk:/MyApp/pytest# python -V
    Python 2.7.13
    root@arm32v7-openjdk:/MyApp/pytest# uname -a
    Linux arm32v7-openjdk 4.9.41-moby #1 SMP Wed Sep 6 00:05:16 UTC 2017 armv7l GNU/Linux
    

    python 代码

    import ctypes
    ll = ctypes.cdll.LoadLibrary
    """
    1 有试过绝对路径和把这个库文件 copy 到 /usr/local/lib/ 目录下
    2 文件肯定存在
    3 权限是 777
    """
    lib = ll("./libencrypt.so")
    
    
    

    报错信息

    root@arm32v7-openjdk:/MyApp/pytest# python test.py
    Traceback (most recent call last):
      File "test.py", line 3, in <module>
        lib = ll("./libencrypt.so")
      File "/usr/lib/python2.7/ctypes/__init__.py", line 440, in LoadLibrary
        return self._dlltype(name)
      File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
        self._handle = _dlopen(self._name, mode)
    OSError: ./libencrypt.so: cannot open shared object file: No such file or directory
    
    8 条回复    2018-05-14 08:53:38 +08:00
    checgg
        1
    checgg  
    OP
       2018-05-13 12:02:36 +08:00
    顺便一提,用 java 写也是报同样的错
    ```
    public class test {
    public static void main(String[] args) {
    System.out.print("Hello world");
    String so = "./libencrypt.so";
    System.load(so);
    }
    }

    ```
    Xiaobaixiao
        2
    Xiaobaixiao  
       2018-05-13 12:24:22 +08:00
    test.py 移动到 libencrypt.so 所在文件夹,或者添加指定执行该命令的文件夹参数试试(如果有)
    checgg
        3
    checgg  
    OP
       2018-05-13 12:40:58 +08:00
    @Xiaobaixiao 是在同目录.
    virusdefender
        4
    virusdefender  
       2018-05-13 12:53:16 +08:00
    strace python test.py 看看系统调用
    wevsty
        5
    wevsty  
       2018-05-13 13:14:07 +08:00
    提示不是很清楚么 cannot open shared object file: No such file or directory
    找不到 libencrypt.so 这个文件,检查路径,工作目录,或者干脆写绝对路径上去。
    rookiebulls
        6
    rookiebulls  
       2018-05-13 13:40:57 +08:00 via iPhone
    路径问题,拿到当前 py 脚本的路径,再 os.path.join 拼一下就可以了
    zsj950618
        7
    zsj950618  
       2018-05-13 14:58:35 +08:00
    ldd libencrypt.so 看下,也有可能是 ld 路径错了。
    izoabr
        8
    izoabr  
       2018-05-14 08:53:38 +08:00
    ctypes.cdll.LoadLibrary 的 path 问题,你试试放到 python 的 lib 目录里去调用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3325 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 11:47 · PVG 19:47 · LAX 04:47 · JFK 07:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.