新版 MacOS 无法从 /usr/local/lib 加载动态链接库的解决办法

自己编写的动态链接库在Unix规范下一般位于/usr/local/lib,在2023年及之前的MacOS版本中,直接将动态库安装到该位置即可在程序运行时加载,可是升级MacOS版本后,ld就报错。

错误现象

运行程序,报错

复制代码
dyld[6376]: Library not loaded: libxxx.dylib
  Referenced from: <BD86A23A-E5C5-35D0-BE6D-F9B966A01280> ..../build/xxxx
  Reason: tried: 'libxxx.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibxxx.dylib' (no such file), 'libxxx.dylib' (no such file), '..../build/libxxx.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/..../build/libxxx.dylib' (no such file), '..../build/libxxx.dylib' (no such file)
zsh: abort      ./xxxx

原因分析

运行

bash 复制代码
man dyld

可以看到以下文本

复制代码
DYLD_FALLBACK_LIBRARY_PATH
       This is a colon separated list of directories that contain libraries.  If a dylib is not found at its
       install  path, dyld uses this as a list of directories to search for the dylib.

       For new binaries (Fall 2023 or later) there is no default.  For older binaries, their is a default fallback
       search path of: /usr/local/lib:/usr/lib.

意思是说2023年秋之后,/usr/local/lib:/usr/lib不再是DYLD_FALLBACK_LIBRARY_PATH的默认值,所以造成了这个重大的行为改变。

解决方法

将该环境变量export赋值,放到.bashrc/.zshrc在shell开启时自动执行(具体操作略)或在程序启动时手动声明,如

bash 复制代码
DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib ./xxxx

即可使程序加载到/usr/local/lib下的动态库,成功运行。

相关推荐
2401_8920709815 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
yuzhuanhei16 小时前
Visual Studio 配置C++opencv
c++·学习·visual studio
qq_3395548216 小时前
英飞凌ModusToolbox环境搭建
c语言·eclipse
不爱吃炸鸡柳16 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发16 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
‎ദ്ദിᵔ.˛.ᵔ₎16 小时前
STL 栈 队列
开发语言·c++
张張40817 小时前
(域格)环境搭建和编译
c语言·开发语言·python·ai
2401_8920709817 小时前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象
郭涤生17 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿17 小时前
vector
c语言·开发语言·数据结构·c++·算法