问题

操作系统环境:Ubuntu 22.04

在安装一个工具应用时,这个应用使用到了chacha20加密算法,所以需要依赖libsodium。按照网上教程下载源码并编译安装:

sudo apt install build-essential
wget https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz
tar xf libsodium-1.0.18.tar.gz
cd libsodium-1.0.18
./configure
make
sudo make install
ldconfig

当启动该工具应用时出现报错:

File "/home/ubuntu/.local/lib/python3.7/site-packages/*****/crypto/util.py", line 80, in find_librarypath = ctypes.util.find_library(name)File "/usr/lib/python3.7/ctypes/util.py", line 341, in find_library_get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))File "/usr/lib/python3.7/ctypes/util.py", line 147, in _findLib_gccif not _is_elf(file):File "/usr/lib/python3.7/ctypes/util.py", line 99, in _is_elfwith open(filename, 'br') as thefile:
FileNotFoundError: [Errno 2] No such file or directory: b'liblibsodium.a'

解决方法

首先确认了libsodium的安装过程,整个安装步骤没有报错,根据安装日志,也在/usr/local/lib/下找到了libsodium.a文件。
网上搜索这个问题,遇到同一问题的人很少,但是却有很多人遇到另一个库"liblibc.a"的报错,他们的解决方案是在libc.a的同一目录下建立软连接liblibc.a,抱着试一试的心态,我在/usr/local/lib下建立了一个软连接

cd /usr/local/lib
sudo ln -s libsodium.a liblibsodium.a

这时候再运行应用,果然运行正常。

问题原因分析

根据报错日志

 File "/usr/lib/python3.7/ctypes/util.py", line 341, in find_library_get_soname(_findLib_gcc(name)) or _get_soname(_findLib_ld(name))File "/usr/lib/python3.7/ctypes/util.py", line 147, in _findLib_gccif not _is_elf(file):File "/usr/lib/python3.7/ctypes/util.py", line 99, in _is_elfwith open(filename, 'br') as thefile:

我们从下往上,定位是在哪一步添加了多出来的lib前缀。
通过分析源码,我们可以发现

elif os.name == "posix":# Andreas Degert's find functions, using gcc, /sbin/ldconfig, objdumpimport re, tempfile........def _findLib_gcc(name):# Run GCC's linker with the -t (aka --trace) option and examine the# library name it prints out. The GCC command will fail because we# haven't supplied a proper program with main(), but that does not# matter.expr = os.fsencode(r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name))........res = re.findall(expr, trace)if not res:return Nonefor file in res:# Check if the given file is an elf file: gcc can report# some files that are linker scripts and not actual# shared objects. See bpo-41976 for more detailsif not _is_elf(file):continuereturn os.fsdecode(file)

在报错日志中调用的_findLib_gcc方法中,是通过正则表达式来查找库文件的,而这里的正则表达式:

 expr = os.fsencode(r'[^\(\)\s]*lib%s\.[^\(\)\s]*' % re.escape(name))

已经自动在库名称前添加了lib前缀。同样的根据python官方文档

ctypes.util.find_library(name)
Try to find a library and return a pathname. name is the library name without any prefix like lib, suffix like .so, .dylib or version number (this is the form used for the posix linker option -l). If no library can be found, returns None.
The exact functionality is system dependent.

要求name中是不能含有lib前缀的。所以这时候我们要检查应用在调用find_library(name)时,传入的name是否含有额外的lib前缀了。
查看find_library的调用源码

def find_library(possible_lib_names, search_symbol, library_name,custom_path=None):import ctypes.util........lib_names = []for lib_name in possible_lib_names:lib_names.append(lib_name)lib_names.append('lib' + lib_name)for name in lib_names:if os.name == "nt":paths.extend(find_library_nt(name))else:path = ctypes.util.find_library(name)if path:paths.append(path)

果然是有一个加lib前缀的操作

lib_names.append('lib' + lib_name)

我们只要注释掉这行代码问题就能解决了。所以这是一个find_library调用不规范引起的报错。

补充

有的电脑上不会发生这个问题,因为还有一个逻辑间接导致的问题的出现,我这台电脑正好是多重因素赶上了。
即使加了多余的前缀lib,在执行re.findall(expr, trace)方法,大部分电脑返回的结果应该是空,但是我这台电脑trace里正巧有个报错信息,含有liblibsodium字段,导致正则表达式匹配出来的结果不为空,才会导致后续的打开文件失败。

b'/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o\n
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/crti.o\n
/usr/lib/gcc/x86_64-linux-gnu/11/crtbeginS.o\n
/usr/bin/ld: cannot find -llibsodium: No such file or directory\n
/usr/bin/ld: note to link with /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libsodium.a use -l:libsodium.a or rename it to liblibsodium.a\n
/usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a\n
/usr/lib/gcc/x86_64-linux-gnu/11/libgcc_s.so\n
/usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/libgcc_s.so.1\n
/usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a\n
collect2: error: ld returned 1 exit status\n'

参考:
ctypes.util.find_library(“libc”) fails
re — Regular expression operations

libsodium引用报错FileNotFoundError: [Errno 2] No such file or directory: b‘liblibsodium.a‘相关推荐

  1. python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory

    项目场景: python读取csv文件路径正确但报错FileNotFoundError: [Errno 2] No such file or directory 问题描述: #储存调用表格.数据 #调 ...

  2. python解压函数extractall在windows上报错FileNotFoundError [Errno 2] No such file or directory

    今天遇到一个很奇葩的问题,使用pyhon脚本下载服务器上的一个打包工具到本地,然后解压到本地使用,这个脚本在我个人电脑上可以正常运行,但是在别的电脑上就报错,报错信息如下: FileNotFoundE ...

  3. 4种方法转义字符解决报错FileNotFoundError: [Errno 2] No such file or directory

    写在前面: 前面的案例不想看可以直接看结尾的4个结论. 直接看第4个结论,就是文章的最后.二八定律.[方法4通用性极强] 快速跳转至 通用性方法4 QQ:1981791622. 备注:我是CSDN道友 ...

  4. 解决python报错FileNotFoundError: [Errno 2] No such file or directory

    原因: 仔细检查代码,原因是要创建的文件所在目录不存在导致的. 即:要创建/a/b/c/d.txt, 则需要先保证/a/b/c目录存在. 代码中先检测目录是否存在,不存在则先创建多级目录: if no ...

  5. python 文件读取错误之FileNotFoundError: [Errno 2] No such file or directory:,顺便学习斜杠/和反斜杠\的用法

    python 文件读取错误之FileNotFoundError: [Errno 2] No such file or directory:,顺便学习斜杠/和反斜杠\的用法: 最近学习文件读取和中文分词 ...

  6. FileNotFoundError: [Errno 2] No such file or directory:‘image.jpg’

    使用自己的主机远程连接服务器运行下面这段程序时, import os from PIL import Image file_dir = '/我的路径/...' for file in os.listd ...

  7. FileNotFoundError: [Errno 2] No such file or directory: 'XXX' 的解决方法

    错误描述: FileNotFoundError: [Errno 2] No such file or directory: 'XXX' 的解决方法 在编写爬虫文件的过程中,一般会将爬取下来的文件保存在 ...

  8. python打开文件时,找不到文件 FileNotFoundError: [Errno 2] No such file or directory报错

    小白一枚,记录学习错误,共同进步 python打开文件时,找不到文件 FileNotFoundError: [Errno 2] No such file or directory报错 UnicodeD ...

  9. 读取文件报错:FileNotFoundError: [Errno 2] No such file or directory

    文章目录 问题描述 问题分析 解决办法 问题描述 使用 img = Image.open('data/DSC_8923.jpg') 读取一张图片时,报 FileNotFoundError: [Errn ...

最新文章

  1. 值得收藏!基于激光雷达数据的深度学习目标检测方法大合集(下)
  2. mysql ef6 事务_使用事务-EF6 | Microsoft Docs
  3. 编程软件python怎样开始学-Python 3.7从零开始学
  4. python爬虫详细步骤-Python爬虫实践入门,超详细
  5. 消息映射的服务器的设计与实现
  6. 3dmax高版本转低版本插件_Fundebug前端JavaScript插件更新至1.8.0,兼容低版本的Android浏览器...
  7. Oracle 抢人了!近 4000 万年薪只为一个 AI 专家
  8. 护航敏捷开发和运维 BCS2020举办DevSecOps论坛
  9. java park_我可以在纯Java中实现park / unpark方法吗?
  10. dts双轨制会员积分系统
  11. 软考中级数据库系统工程师备考经验分享
  12. Ubuntu Linux 下 Ffmpeg 及 Mencoder 安装使用小结
  13. 咪蒙注销后,我用Python对其1013篇历史文章做了一次深度的数据分析...
  14. FreeSWITCH技巧:实现短消息发送
  15. 冷笑话,笑不笑随你,哈哈!
  16. vcm驱动芯片原理_每周一品 · 音圈电机(VCM)中的磁性材料
  17. viper12a电路图_viper12a电磁炉电路图
  18. bootstrap自学总结不间断更新
  19. CSS padding(填充)
  20. 腾讯云SDK使用python版

热门文章

  1. (00XX系列)摸摸Windows的SEH
  2. 已经摆在面前了,该不该上
  3. OBS插件开发以及OBS插件的选择(obs直播插件)研究思路
  4. 工具类 - jetson录屏工具和串口调试工具
  5. Android-自定义心电图控件
  6. 送书福利 |《趣味编程三剑客:从Scratch迈向Python和C++》
  7. python--re模块--黑科技
  8. Python中的图像处理(第九章)Python图像增强
  9. 淘客基地淘客微信公众号系统代理合伙人更新至1.2.3
  10. java list最后一个元素_Java 8 Stream List 获取最后一个元素