使用Valgrind分析Rust应用时如何指定SO库路径?
问题
在搭载Intel芯片的macOS 13.4.1系统上,使用Valgrind分析Rust应用时执行命令出现动态库加载错误,执行命令及报错信息如下:
~/source/reddwarf/backend/texhub-server/target/debug on main! ⌚ 19:34:50 $ valgrind ./texhub-server ‹ruby-2.7.2› ==44422== Memcheck, a memory error detector ==44422== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al. ==44422== Using Valgrind-3.22.0.GIT-lbmacos and LibVEX; rerun with -h for copyright info ==44422== Command: ./texhub-server ==44422== --44422-- run: /usr/bin/dsymutil "./texhub-server" dyld[44422]: Library not loaded: libsynctex_parser.so Referenced from: <7EF21E86-72BE-30C2-B78A-4E8187973103> /Users/dolphin/source/reddwarf/backend/texhub-server/target/debug Reason: tried: 'libsynctex_parser.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibsynctex_parser.so' (no such file), 'libsynctex_parser.so' (no such file), '/usr/local/lib/libsynctex_parser.so' (no such file), '/usr/lib/libsynctex_parser.so' (no such file, not in dyld cache), '/Users/dolphin/source/reddwarf/backend/texhub-server/target/debug/libsynctex_parser.so' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/Users/dolphin/source/reddwarf/backend/texhub-server/target/debug/libsynctex_parser.so' (no such file), '/Users/dolphin/source/reddwarf/backend/texhub-server/target/debug/libsynctex_parser.so' (no such file), '/usr/local/lib/libsynctex_parser.so' (no such file), '/usr/lib/libsynctex_parser.so' (no such file, not in dyld cache) [1] 44422 killed valgrind ./texhub-server (base)
已知libsynctex_parser.so的路径为~/source/reddwarf/backend/texhub-server/src/so,Valgrind版本为valgrind-3.22.0.GIT-lbmacos,询问是否可在运行Valgrind命令时指定该SO库路径。
解决方案
可以通过设置DYLD_LIBRARY_PATH环境变量指定动态库搜索路径,运行Valgrind时一并传入即可,具体命令如下:
DYLD_LIBRARY_PATH=~/source/reddwarf/backend/texhub-server/src/so valgrind ./texhub-server
若路径包含特殊字符,建议使用绝对路径执行:
DYLD_LIBRARY_PATH=/Users/dolphin/source/reddwarf/backend/texhub-server/src/so valgrind ./texhub-server
macOS的安全机制可能会限制DYLD_LIBRARY_PATH的作用范围,但针对本地编译程序和自定义库,这种方式通常可正常工作。如果仍有问题,不建议将库文件复制到/usr/local/lib等系统默认路径(会污染系统目录),可选择在编译Rust程序时指定rpath,让程序自身能找到库路径:
在Cargo.toml中添加如下配置:
[build] rustflags = ["-C", "link-arg=-Wl,-rpath,/Users/dolphin/source/reddwarf/backend/texhub-server/src/so"]
编译后的程序会自动在指定路径搜索动态库,后续运行Valgrind时无需额外设置环境变量。
内容的提问来源于stack exchange,提问作者Dolphin
相关产品推荐
相关产品推荐

