日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2024/3/24 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 libsodium引用报错FileNotFoundError: [Errno 2] No such file or directory: b‘liblibsodium.a‘ 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問題

操作系統(tǒng)環(huán)境:Ubuntu 22.04

在安裝一個(gè)工具應(yīng)用時(shí),這個(gè)應(yīng)用使用到了chacha20加密算法,所以需要依賴libsodium。按照網(wǎng)上教程下載源碼并編譯安裝:

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

當(dāng)啟動(dòng)該工具應(yīng)用時(shí)出現(xiàn)報(bào)錯(cuò):

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'

解決方法

首先確認(rèn)了libsodium的安裝過程,整個(gè)安裝步驟沒有報(bào)錯(cuò),根據(jù)安裝日志,也在/usr/local/lib/下找到了libsodium.a文件。
網(wǎng)上搜索這個(gè)問題,遇到同一問題的人很少,但是卻有很多人遇到另一個(gè)庫(kù)"liblibc.a"的報(bào)錯(cuò),他們的解決方案是在libc.a的同一目錄下建立軟連接liblibc.a,抱著試一試的心態(tài),我在/usr/local/lib下建立了一個(gè)軟連接

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

這時(shí)候再運(yùn)行應(yīng)用,果然運(yùn)行正常。

問題原因分析

根據(jù)報(bào)錯(cuò)日志

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:

我們從下往上,定位是在哪一步添加了多出來(lái)的lib前綴。
通過分析源碼,我們可以發(fā)現(xiàn)

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)

在報(bào)錯(cuò)日志中調(diào)用的_findLib_gcc方法中,是通過正則表達(dá)式來(lái)查找?guī)煳募?#xff0c;而這里的正則表達(dá)式:

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

已經(jīng)自動(dòng)在庫(kù)名稱前添加了lib前綴。同樣的根據(jù)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中是不能含有l(wèi)ib前綴的。所以這時(shí)候我們要檢查應(yīng)用在調(diào)用find_library(name)時(shí),傳入的name是否含有額外的lib前綴了。
查看find_library的調(diào)用源碼

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)

果然是有一個(gè)加lib前綴的操作

lib_names.append('lib' + lib_name)

我們只要注釋掉這行代碼問題就能解決了。所以這是一個(gè)find_library調(diào)用不規(guī)范引起的報(bào)錯(cuò)。

補(bǔ)充

有的電腦上不會(huì)發(fā)生這個(gè)問題,因?yàn)檫€有一個(gè)邏輯間接導(dǎo)致的問題的出現(xiàn),我這臺(tái)電腦正好是多重因素趕上了。
即使加了多余的前綴lib,在執(zhí)行re.findall(expr, trace)方法,大部分電腦返回的結(jié)果應(yīng)該是空,但是我這臺(tái)電腦trace里正巧有個(gè)報(bào)錯(cuò)信息,含有l(wèi)iblibsodium字段,導(dǎo)致正則表達(dá)式匹配出來(lái)的結(jié)果不為空,才會(huì)導(dǎo)致后續(xù)的打開文件失敗。

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(“l(fā)ibc”) fails
re — Regular expression operations

總結(jié)

以上是生活随笔為你收集整理的libsodium引用报错FileNotFoundError: [Errno 2] No such file or directory: b‘liblibsodium.a‘的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。