新版 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下的动态库,成功运行。

相关推荐
云栖梦泽21 小时前
Linux内核与驱动:13.从设备树到Platform平台总线
linux·运维·c++·嵌入式硬件
qeen8721 小时前
【算法笔记】模拟与高精度加减乘除
c++·笔记·算法·高精度·模拟
txinyu的博客21 小时前
高并发内存池 - 简化版 tcmalloc
c++
charlie11451419121 小时前
嵌入式Linux驱动开发指南02——内核空间基础与硬件访问
linux·运维·c语言·驱动开发·嵌入式硬件
少司府21 小时前
C++基础入门:内存管理
c语言·开发语言·c++·内存管理·delete·new·malloc
鱼很腾apoc1 天前
【学习篇】第17期 C++入门必看——类和对象全站最详篇
c语言·开发语言·学习·算法·青少年编程
Sakuyu434681 天前
C语言基础(一)
c语言·开发语言
码农的神经元1 天前
2026 MathorCup C 题实战复盘:从高血脂风险预警到 6 个月干预优化的建模思路与 Python 落地
c语言·开发语言·python
郝学胜-神的一滴1 天前
从零起步:CMake基础入门与实战跨平台编译
c++·软件工程·软件构建·cmake
charlie1145141911 天前
嵌入式现代C++工程实践——第14篇:第二次重构 —— 模板登场,编译时绑定端口和引脚
开发语言·c++·stm32·安全·重构