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

2018 年 5 月 13 日
 checgg

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
3809 次点击
所在节点    问与答
8 条回复
checgg
2018 年 5 月 13 日
顺便一提,用 java 写也是报同样的错
```
public class test {
public static void main(String[] args) {
System.out.print("Hello world");
String so = "./libencrypt.so";
System.load(so);
}
}

```
Xiaobaixiao
2018 年 5 月 13 日
test.py 移动到 libencrypt.so 所在文件夹,或者添加指定执行该命令的文件夹参数试试(如果有)
checgg
2018 年 5 月 13 日
@Xiaobaixiao 是在同目录.
virusdefender
2018 年 5 月 13 日
strace python test.py 看看系统调用
wevsty
2018 年 5 月 13 日
提示不是很清楚么 cannot open shared object file: No such file or directory
找不到 libencrypt.so 这个文件,检查路径,工作目录,或者干脆写绝对路径上去。
rookiebulls
2018 年 5 月 13 日
路径问题,拿到当前 py 脚本的路径,再 os.path.join 拼一下就可以了
zhsj
2018 年 5 月 13 日
ldd libencrypt.so 看下,也有可能是 ld 路径错了。
izoabr
2018 年 5 月 14 日
ctypes.cdll.LoadLibrary 的 path 问题,你试试放到 python 的 lib 目录里去调用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/454444

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX