Mac下使用macdeployqt打包qt程序:

概述

初次在Mac上使用qt的macdeployqt来打包生成的可执行程序,这里记录下。由于我的程序是调用之前生成的qt动态库,而动态库又依赖于第三方库,相对于没有库文件依赖的程序,这里有一些需要注意的点,下面是打包的步骤。

打包步骤

使用macdeployqt打包qt程序:
1.项目设置为release模式,编译并运行生成release下的可执行程序;
2.将1中生成的可执行程序,存入一个空的文件夹下;
3.在终端zsh中进入到qt安装目录下,找到macdeployqt,并cd到macdeployqt所在的目录下;
我的macdeployqt所在的目录是:/Users/admin/Qt/6.2.2/macos/bin/macdeployqt
4.执行指令:./macdeployqt xxx.app;
这里所在的目录是macdeployqt所在目录,所以需要可执行程序的绝对路径,即完整路径,此处输入指令:
./macdeployqt /Users/admin/Documents/qt-program/dll-testDll/testDll.app;
可执行程序中要是使用了qml模块,则相应的打包指令变为:macdeployqt xxx.app -qmldir=xxx/xxx/xxx/xxx/,
其中xxx.app使用的绝对路径,-qmldir的赋值是qml文件所在的路径,这里指出qml文件所在的文件夹路径即可,
如我的程序里使用了qml则输入的指令为:./macdeployqt /Users/admin/Documents/qt-program/dll-testDll/testDll.app -qmldir=/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/controls/,我的电脑是m1芯片,在使用macdeployqt打包程序的时候,会出现下面的输出:

ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtWidgets.framework/Versions/A/QtWidgets (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtGui.framework/Versions/A/QtGui (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtCore.framework/Versions/A/QtCore (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtQuickWidgets.framework/Versions/A/QtQuickWidgets (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtQuick.framework/Versions/A/QtQuick (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtOpenGL.framework/Versions/A/QtOpenGL (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtWidgets.framework/Versions/A/QtWidgets (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtGui.framework/Versions/A/QtGui (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtQmlModels.framework/Versions/A/QtQmlModels (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtQml.framework/Versions/A/QtQml (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtNetwork.framework/Versions/A/QtNetwork (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtCore.framework/Versions/A/QtCore (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/PlugIns/platforms/libqcocoa.dylib (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtGui.framework/Versions/A/QtGui (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtCore.framework/Versions/A/QtCore (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtGui.framework/Versions/A/QtGui (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Qt/6.2.2/macos/lib/QtCore.framework/Versions/A/QtCore (architecture arm64):"
ERROR: Could not parse otool output line: "/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/PlugIns/styles/libqmacstyle.dylib (architecture arm64):"

以上只是拷贝了其输出的一部分,第一次看到这个的时候,一直在纠结如何解决这个问题,后面发现即使看上去这个输出是有错误的,但是可执行程序依旧可以实现打包,此时可以双击打开,对于只是简单的可执行程序,没有依赖第三方库的程序,此时可以尝试双击打开可执行程序,也就是说这个显示有错误的输出不影响最终生成的可执行程序。
备注:若生成的可执行程序,没有调用第三方库,则按照上面1,2,3步骤便可以打包完成,双击运行;若是生成的可执行程序调用了动态库,则在1,2,3的基础上还需要再将库文件做相应的配置;
5.查看生成的可执行程序需要依赖的动态库。
终端zsh下使用指令otool -L xxx.app;这里使用可执行程序的完整的路径;
如我生成的可执行程序所在的路径为:/Users/admin/Documents/qt-program/dll-testDll/testDll.app,右键在testDll.app上出现右键菜单,选择查看文件内容,会出现文件夹Contents,进入后可以看到MacOS文件夹,在MacOS文件夹下是可执行程序testDll 。查看testDll 所依赖的动态库指令为:
otool -L /Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/testDll
可以看到:

 otool -L /Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/testDll 
/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/testDll:
	libtestAgoraVideoDll.1.dylib (compatibility version 1.0.0, current version 1.0.0)
	@rpath/QtWidgets.framework/Versions/A/QtWidgets (compatibility version 6.0.0, current version 6.2.2)
	@rpath/QtGui.framework/Versions/A/QtGui (compatibility version 6.0.0, current version 6.2.2)
	/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit (compatibility version 45.0.0, current version 2113.20.111)
	/System/Library/Frameworks/ImageIO.framework/Versions/A/ImageIO (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Metal.framework/Versions/A/Metal (compatibility version 1.0.0, current version 258.17.0)
	@rpath/QtCore.framework/Versions/A/QtCore (compatibility version 6.0.0, current version 6.2.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/AGL.framework/Versions/A/AGL (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL (compatibility version 1.0.0, current version 1.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1200.3.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.0.0)

以上是testDll所依赖的库文件,其中有些文件指明了路径,像/System/开头,/usr/开头的,一般是系统路径,会自动在这些路径下查找相应的库文件,无需考虑,@rpath为运行路径,因为我的可执行程序下的testDll.app在右键打开进入Contents文件夹下后可以看到如下:
请添加图片描述
其中Frameworks文件夹和Plugins文件夹是打包后生成的。Frameworks文件夹下存放的是可执行程序依赖于qt的一些库,Plugins指明了一些插件。由于之前通过otool -L查看了可执行程序testDll的依赖库,接下来将可执行程序调用的库文件libtestAgoraVideoDll.1.dylib,进行一些配置和放置。
6.修改libtestAgoraVideoDll.1.dylib的查找路径,使可执行程序在可执行程序所在的同级目录下查找ibtestAgoraVideoDll.1.dylib。
使用指令:
install_name_tool -change “libtestAgoraVideoDll.1.dylib” “@executable_path/libtestAgoraVideoDll.1.dylib” /Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/testDll
7.拷贝ibtestAgoraVideoDll.1.dylib库文件到可执行程序testDll所在的目录下;
请添加图片描述
8.由于程序调用了生成的库ibtestAgoraVideoDll.1.dylib,而ibtestAgoraVideoDll.1.dylib又依赖于第三方库,所以放置好ibtestAgoraVideoDll.1.dylib后还需要设置ibtestAgoraVideoDll.1.dylib依赖的第三方库的路径。
这里将ibtestAgoraVideoDll.1.dylib所依赖的第三方库文件拷贝到文件夹/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/Frameworks下;
9.程序调用生成的qt动态库,该动态库使用了qml文件来实现界面。
所以还需将qml文件做一些放置和配置;
该实现指令同上述的第4条提到的./macdeployqt /Users/admin/Documents/qt-program/dll-testDll/testDll.app -qmldir=/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/controls/,最好在第4条执行该语句,执行该语句前将生成动态库所需要的qml文件拷贝到路径/Users/admin/Documents/qt-program/dll-testDll/testDll.app/Contents/MacOS/controls/文件夹下。如果没有将用到的qml文件做配置和放置,会出现运行程序后只有一个白色的窗口。
9.以上打包一个调用依赖于第三方库生成的qt动态库的程序生成了。但是双击可执行程序不一定能正常运行,取决于有没有使用一些mac认为的比较敏感的数据,比如照相机,麦克风,通讯录之类,需要根据程序运行时出现的奔溃报告在info.plist文件中添加相应的key和value。我的程序添加的是照相机和麦克风。之后便能正常运行了。
在这里插入图片描述
info.plist中新加的内容如下:
在这里插入图片描述
以上是关于mac下使用qt打包程序的一点记录。

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果 Qt::WindowTaskbarButtonHint 属性在你使用Qt 版本中不可用,可以考虑使用 Qt::Tool 属性来实现窗口分开的效果。Qt::Tool 属性将窗口转换为工具栏窗口,可以在任务栏中分别显示每个窗口的图标和标题。 具体实现方法如下: 1. 在需要设置的窗口类的构造函数中,通过 setWindowFlags 函数设置窗口属性。例如,设置为工具栏窗口: ``` c++ setWindowFlags(Qt::Tool | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowStaysOnTopHint); ``` 2. 如果希望窗口合并到同一任务栏图标下,可以将 Qt::Tool 属性去掉即可。 ``` c++ setWindowFlags(Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowStaysOnTopHint); ``` 示例代码如下: ``` c++ #include <QApplication> #include <QWidget> int main(int argc, char *argv[]) { QApplication app(argc, argv); QWidget window; window.setWindowFlags(Qt::Tool | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowStaysOnTopHint); window.show(); QWidget window2; window2.setWindowFlags(Qt::Tool | Qt::WindowMinimizeButtonHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowStaysOnTopHint); window2.show(); return app.exec(); } ``` 注意:使用 Qt::Tool 属性将窗口转换为工具栏窗口,可能会导致窗口的行为和样式与普通窗口不同。例如,工具栏窗口默认不会显示在 Windows 任务栏和 Mac OS Dock 中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肩上风骋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值