Qt5迁移至Qt6.11(macOS+QMake)遇mach-o文件类型链接错误
macOS下QMake迁移Qt5到Qt6.11时链接Qt自身dylib报错
环境与配置
- QTDIR路径:
/Users/me/Qt6/6.11.1/macos - .pro文件配置:
QT += core gui help uitools widgets printsupport network xml sql Platforms.files += $$(QTDIR)/plugins/platforms/libqcocoa.dylib Styles.files += $$(QTDIR)/plugins/styles/libqmacstyle.dylib Imageformats.files += $$(QTDIR)/plugins/imageformats/libqico.dylib \ $$(QTDIR)/plugins/imageformats/libqgif.dylib \ $$(QTDIR)/plugins/imageformats/libqjpeg.dylib \ $$(QTDIR)/plugins/imageformats/libqsvg.dylib
编译错误信息
unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/platforms/libqcocoa.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/imageformats/libqico.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/styles/libqmacstyle.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/imageformats/libqgif.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/imageformats/libqjpeg.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/imageformats/libqsvg.dylib' unsupported mach-o filetype (only MH_OBJECT and MH_DYLIB can be linked) in '../MyApp.app/Contents/Plugins/sqldrivers/libqsqlite.dylib'
问题原因
Qt6中这些插件文件虽后缀为.dylib,但实际是MH_BUNDLE类型的bundle格式,而非标准的MH_DYLIB动态库。链接器仅支持处理MH_OBJECT(目标文件)和MH_DYLIB(动态库)类型的文件,因此触发报错。此外,Qt6的插件部署逻辑与Qt5不同,手动复制文件会破坏插件的依赖结构与类型识别逻辑。
解决方法
- 移除.pro中手动复制插件的代码,改用Qt官方提供的
macdeployqt工具自动处理部署。这是Qt6推荐的macOS App打包方式,会自动识别插件格式、修复依赖路径,确保所有必要组件正确部署。 - 编译完成后,执行以下命令(替换为你的App路径):
该命令会自动打包所有依赖的插件、框架,生成可直接分发的App或dmg镜像。macdeployqt MyApp.app -dmg - 若需在QMake中手动部署插件,不要直接复制文件,改用Qt的
INSTALLS机制配合官方变量,示例:
但仍建议优先使用platforms.path = $$OUT_PWD/MyApp.app/Contents/PlugIns/platforms platforms.files = $$QT_INSTALL_PLUGINS/platforms/libqcocoa.dylib INSTALLS += platformsmacdeployqt避免依赖问题。 - 可通过
file命令验证插件类型:
输出应为file /Users/me/Qt6/6.11.1/macos/plugins/platforms/libqcocoa.dylibMach-O 64-bit bundle x86_64或Mach-O 64-bit bundle arm64,确认其为bundle格式而非标准动态库。
内容的提问来源于stack exchange,提问作者Pierre
相关产品推荐
相关产品推荐

