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

相关推荐
Cx330❀1 天前
【优选算法必刷100题】第43题(模拟):数青蛙
c++·算法·leetcode·面试
闻缺陷则喜何志丹1 天前
【C++动态规划 状压dp】1879. 两个数组最小的异或值之和|2145
c++·算法·动态规划·力扣·数组·最小·动态规范
艾莉丝努力练剑1 天前
【优选算法必刷100题:专题五】(位运算算法)第033~38题:判断字符是否唯一、丢失的数字、两整数之和、只出现一次的数字 II、消失的两个数字
java·大数据·运维·c++·人工智能·算法·位运算
山上三树1 天前
详细介绍 C 语言中的匿名结构体
c语言·开发语言·算法
EXtreme351 天前
【数据结构】彻底搞懂二叉树:四种遍历逻辑、经典OJ题与递归性能全解析
c语言·数据结构·算法·二叉树·递归
山上三树1 天前
详细介绍 C 语言 typedef 及与 #define 的核心对比
c语言·数据结构·算法
xiaolongmeiya1 天前
P7082 [NWRRC 2013] Dwarf Tower 完全背包
c++
2401_876221341 天前
因数个数、因数和、因数积
c++·算法
十五年专注C++开发1 天前
VS2019编译的C++程序,在win10正常运行,在win7上Debug正常运行,Release运行报错0xC0000005,进不了main函数
开发语言·c++·报错c0x0000005
fy zs1 天前
网络编程套接字
linux·服务器·网络·c++