解决Mac QT隐式调用dylib动态库报错问题Library not loaded Reason: image not found

2022-10-12 17:57:57  阅读 2887 次 评论 0 条

Mac上QT写了个动态库,另个项目用隐式调用,为啥隐式调用,方便,不用一个个加截函数。隐式显式区别及用法此处不讨论。

在QtCreator中Run 可正常启动,但在build目录下直接启动报错,找不到dylib

Termination Reason:    DYLD, [0x1] Library missing
Application Specific Information:
dyld: launch, loading dependent libraries
Dyld Error Message:
  Library not loaded: libUpgrade.1.dylib
  Referenced from: /Users/USER/*/UpgradeInstaller.app/Contents/MacOS/UpgradeInstaller
  Reason: image not found
Binary Images:
       0x10ced0000 -        0x10cee1ffb +com.yourcompany.UpgradeInstaller (0) <FFC5805D-DAAC-3E8A-8177-AA3F02FBC6C8> /Users/USER/*/UpgradeInstaller.app/Contents/MacOS/UpgradeInstaller
       0x10fd19000 -        0x10fda9cb7  dyld (733.8) <EBC07CB6-870A-3A8E-B48A-67F62EA161F3> /usr/lib/dyld

根据windows VS开发经验,尝试将此动态库拷贝到包内 MacOS下执行程序的同级目录,不行……

百度……关键字都没,中国人都没这么玩过吗?

stackoverflow上找到一篇文章https://stackoverflow.com/questions/54731756/link-dylib-in-macos-qt-project

摘选[Richard Barber](https://stackoverflow.com/users/8258079/richard-barber)的回答

/Users/ingoforster/Documents/Development/Playground/build-TestGround-Desktop_Qt_5_9_1_clang_64bit-Debug/TestGround.app/Contents/MacOS/TestGround appears to be compile-linked to your library code but perhaps has not been formally installed to the executable. macdeployqt usually deals with this issue. You may also do this manually with otool and install_name_tool:
otool -L /path/to/executable
This will list the installed paths to the dylibs in use. You will see your dylib, that is the "/old/path/to/libmylib.dylib"
install_name_tool -change /old/path/to/libmylib.dylib /new/path/to/libmylib.dylib /path/to/executable
Usually, in an .app, the dylib is installed to a subdirectory within the .app folder.
Application.app/
    Contents/
        MacOS/
            executable
        Frameworks/
            libmylib.dylib
With install_name_tool you can point to the dylib relative to the executable path to make the app portable. install_name_tool -change /old/path/to/libmylib.dylib @executable_path/../Frameworks/libmylib.dylib /path/to/executable

解决方法:

用 otool -L /path/XXX.app/Contents/MacOS/executable 得到

libUpgrade.1.dylib (compatibility version 1.0.0, current version 1.0.0)
@rpath/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.14.0, current version 5.14.2)
@rpath/QtGui.framework/Versions/5/QtGui (compatibility version 5.14.0, current version 5.14.2)
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 1894.40.150)
/System/Library/Frameworks/Metal.framework/Versions/A/Metal (compatibility version 1.0.0, current version 212.5.15)
@rpath/QtNetwork.framework/Versions/5/QtNetwork (compatibility version 5.14.0, current version 5.14.2)
@rpath/QtCore.framework/Versions/5/QtCore (compatibility version 5.14.0, current version 5.14.2)
/System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
/System/Library/Frameworks/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 902.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)

第一个libUpgrade.1.dylib即为我自己的动态库,看到它是没有路径的,系统就会去/usr/lib 等系统目录去找,自然找不到

用这改路径,参照大神的例子install_name_tool -change /old/path/to/libmylib.dylib /new/path/to/libmylib.dylib /path/to/executable

第一个路径我的库没有路径,直接写库名,参考otool工具打印的结果

第二个路径 @executable_path就这么写,改成自己的库名

第三个为/Contents/MacOS下的执行程序的全路径

install_name_tool -change libUpgrade.1.dylib @executable_path/../Frameworks/libUpgrade.1.dylib /path/Contents/MacOS/executable 

改成功后再otool 工具查看,第一个库的路径变了

@executable_path/../Frameworks/libUpgrade.1.dylib (compatibility version 1.0.0, current version 1.0.0)
@rpath/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.14.0, current version 5.14.2)
省略……

再进入到包里面……忽然想到可能有人不知道怎么进。右键你的APP,点“显示包内容”


建个Frameworks目录,把库拷进去,运行成功!!!!

再用 macdeployqt 命令打成dmg包,就可以发给小伙伴玩啦

macdeployqt /Users/bloguan/QtProjects/build/build-UpgradeInstaller-Desktop_Qt_5_14_2_clang_64bit-Debug/UpgradeInstaller.app -dmg

对了,重新编译后,刚修改的路径就失效了,你得重来一次,所以,正常开发还是用QtCreator直接跑吧,这操作在发布前搞一次就行了


本文地址:http://bloguan.com/?id=592
版权声明:本文为原创文章,版权归 博观网 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?