MacOS - QT动态库打包嵌入问题处理

发布时间 2023-12-29 11:50:21作者: [BORUTO]

Crash信息

Termination Reason: Namespace DYLD, Code 1 Library missing
Library not loaded: @rpath/QtCore.framework/Versions/5/QtCore
Referenced from: /Applications/AppName.app/Contents/MacOS/AppName
Reason: tried: '/Applications/AppName.app/Contents/MacOS/../Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/Applications/AppName.app/Contents/MacOS/../Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/Applications/5.12.12/5.12.12/clang_64/lib/QtCore.framework/Versions/5/QtCore' (no such file), '/Applications/AppName.app/Contents/MacOS/../Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/Applications/AppName.app/Contents/MacOS/../Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/Applications/5.12.12/5.12.12/clang_64/lib/QtCore.framework/Versions/5/QtCore' (no such file), '/Library/Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/System/Library/Frameworks/QtCore.framework/Versions/5/QtCore' (no such file)
(terminated at launch; ignore backtrace)

打包后在其它电脑安装后出现了上述问题,找不到QtCore.framework,之前开发一直在自己的开发电脑,该机器是安装了QT环境的,调试跑起来也没注意到这个问题

解决问题

查看工程引入的QT库,原来使用的是静态库,这个版本升级了QT使用的是动态库了,发现没有嵌入,于是果断修改库嵌入方式为Embed & Sign,再次编译

/Users/用户名/Documents/Terminal/trunk/项目名称/ReleaseLib_MAC/AppName.app: unsealed contents present in the root directory of an embedded framework
In subcomponent: /Users/用户名/Documents/Terminal/trunk/项目名称/ReleaseLib_MAC/AppName.app/Contents/Frameworks/QtCore.framework

结果并不是预期的,在Debug环境下始终无法编译,甚至换成Embed Without Signing 也是不行

通常我们的动态库是选择Embed嵌入打包进app的,这个QT库却有点不一样。没办法,于是上网查找了,所幸找到了,解决办法是Xcode工程不嵌入动态库,也就是对QT动态库保持Do Not Embed配置。

其次,在Build Phases中添加一个编译脚本

/Users/用户名/QT/5.12.12/clang_64/bin/macdeployqt ${CODESIGNING_FOLDER_PATH}

同时,在Xcode的主target build settingOther Code Signing Flags 添加一个签名标志位 --deep

这里需要使用QT提供的工具处理这几个动态库,在编译打包的时候会把动态库拷贝到app包内的Frameworks文件夹下,这样打包发布就可以正常找到动态库了