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

相关推荐
ptu小鹏3 小时前
类和对象(中)
开发语言·c++
我该如何取个名字4 小时前
Mac mini 安装mysql数据库以及出现的一些问题的解决方案
数据库·mysql·macos
明月看潮生5 小时前
青少年编程与数学 02-018 C++数据结构与算法 06课题、树
数据结构·c++·算法·青少年编程·编程与数学
小指纹5 小时前
动态规划(一)【背包】
c++·算法·动态规划
zhaoyqcsdn6 小时前
抽象工厂模式及其在自动驾驶中的应用举例(c++代码实现)
c++·经验分享·笔记·设计模式
天若有情6736 小时前
用 C++ 模拟 Axios 的 then 方法处理异步网络请求
网络·c++·php
h39747 小时前
MFC文件-写MP4
c++·windows·音视频·mfc
阿让啊8 小时前
单片机获取真实时间的实现方法
c语言·开发语言·arm开发·stm32·单片机·嵌入式硬件
FightingLod8 小时前
STM32版I²C相亲指南(软件硬件双修版)
c语言·stm32·单片机
姝孟8 小时前
学习笔记(C++篇)--- Day 4
c++·笔记·学习