如何使用install_name_tool在Mac OS X上部署Qt应用程序?

时间:2013-08-28 21:07:37

标签: macos qt deployment

我在为Mac OS X打包我的Qt应用程序时遇到问题。

我已经阅读了Deploying an Application on Mac OS X上的文档,但我仍然不确定我做错了什么。

在我的Mac上,我在〜/ Qt5.1.0 / 5.1.0 / clang_64上安装了Qt5(这是bin /和lib /文件夹所在的位置)

我在〜/ Documents / QtProjects / renamer /上有一个名为“renamer”的Qt应用程序。

使用Qt Creator,我在〜/ Documents / QtProjects / build-renamer-Desktop_Qt_5_1_0_clang_64bit-Release上构建了我的应用程序的发布版本。 所以我做的第一件事是在我的应用程序上运行otool

$ cd ~/Documents/QtProjects/build-renamer-Desktop_Qt_5_1_0_clang_64bit-Release
$ otool -L renamer.app/Contents/MacOS/renamer

结果如下:

renamer.app/Contents/MacOS/renamer:
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.1.0, current version 5.1.0)
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtGui.framework/Versions/5/QtGui (compatibility version 5.1.0, current version 5.1.0)
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtCore.framework/Versions/5/QtCore (compatibility version 5.1.0, current version 5.1.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/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

我接下来要做的是在renamer.app包中创建一个Frameworks目录,并将QtWidgets,QtGui和QtCore框架复制到新目录中:

$ cd ~/Documents/QtProjects/build-renamer-Desktop_Qt_5_1_0_clang_64bit-Release
$ mkdir renamer.app/Contents/Frameworks
$ cp -R ~/Qt5.1.0/5.1.0/clang_64/lib/QtCore.framework renamer.app/Contents/Frameworks
$ cp -R ~/Qt5.1.0/5.1.0/clang_64/lib/QtGui.framework renamer.app/Contents/Frameworks
$ cp -R ~/Qt5.1.0/5.1.0/clang_64/lib/QtWidgets.framework renamer.app/Contents/Frameworks

然后我运行install_name_tool来设置QtWidgets,QtGui和QtCore框架的标识名称:

$ cd ~/Documents/QtProjects/build-renamer-Desktop_Qt_5_1_0_clang_64bit-Release
$ install_name_tool -id @executable_path/../Frameworks/QtCore.framework/Versions/5/QtCore "renamer.app/Contents/Frameworks/QtCore.framework/Versions/5/QtCore"
$ install_name_tool -id @executable_path/../Frameworks/QtGui.framework/Versions/5/QtGui "renamer.app/Contents/Frameworks/QtGui.framework/Versions/5/QtGui"
$ install_name_tool -id @executable_path/../Frameworks/QtWidgets.framework/Versions/5/QtWidgets "renamer.app/Contents/Frameworks/QtWidgets.framework/Versions/5/QtWidgets"

然后我确保应用程序知道在哪里找到库:

$ cd ~/Documents/QtProjects/build-renamer-Desktop_Qt_5_1_0_clang_64bit-Release
$ install_name_tool -change /../Frameworks/QtCore.framework/Versions/5/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/5/QtCore "renamer.app/Contents/MacOS/renamer"
$ install_name_tool -change /../Frameworks/QtGui.framework/Versions/5/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/5/QtGui "renamer.app/Contents/MacOS/renamer"
$ install_name_tool -change /../Frameworks/QtWidgets.framework/Versions/5/QtWidgets @executable_path/../Frameworks/QtWidgets.framework/Versions/5/QtWidgets "renamer.app/Contents/MacOS/renamer"

最后,由于QtGui框架依赖于QtCore而QtWidgets框架依赖于QtGui / QtCore,我还更改了QtGui和QtWidgets的引用:

$ install_name_tool -change /../Frameworks/QtCore.framework/Versions/5/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/5/QtCore "renamer.app/Contents/Frameworks/QtGui.framework/Versions/5/QtGui"
$ install_name_tool -change /../Frameworks/QtGui.framework/Versions/5/QtGui @executable_path/../Frameworks/QtGui.framework/Versions/5/QtGui "renamer.app/Contents/Frameworks/QtWidgets.framework/Versions/5/QtWidgets"
$ install_name_tool -change /../Frameworks/QtCore.framework/Versions/5/QtCore @executable_path/../Frameworks/QtCore.framework/Versions/5/QtCore "renamer.app/Contents/Frameworks/QtWidgets.framework/Versions/5/QtWidgets"

然而,当我之后再次运行otool -L renamer.app/Contents/MacOS/renamer时,没有任何变化,我得到与以前相同的输出:

renamer.app/Contents/MacOS/renamer:
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtWidgets.framework/Versions/5/QtWidgets (compatibility version 5.1.0, current version 5.1.0)
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtGui.framework/Versions/5/QtGui (compatibility version 5.1.0, current version 5.1.0)
    /Users/paul/Qt5.1.0//5.1.0/clang_64/lib/QtCore.framework/Versions/5/QtCore (compatibility version 5.1.0, current version 5.1.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/libstdc++.6.dylib (compatibility version 7.0.0, current version 56.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)

我认为运行otool -L renamer.app/Contents/MacOS/renamer会将输出更改为新的Qt路径,但它仍然是相同的。我以为我遵循了所有步骤,但它不起作用。我错过了一步还是做错了什么?我能够在Windows上成功部署我的程序没有问题,但我在Mac上遇到了麻烦。我怎样才能使运行otool显示新设置的路径,以便从我的renamer.app包中找到QtCore,QtGui和QtWidgets框架?

我还尝试使用macdeployqt来部署我的应用程序,但是当我重命名/删除安装在〜/ Qt5.1.0 / 5.1.0 / clang_64下的Qt库时,我的应用程序无效。在将/ bin添加到PATH后,我在项目文件夹下尝试了命令sudo macdeployqt renamer.appmacdeployqt运行正常,但是当我重命名/删除Qt时,我的应用程序不再运行。运行otool后我也运行了macdeployqt,但输出没有改变。我认为macdeployqt将必要的Qt库复制到应用程序包中,但是没有正确地改变查找qt库的位置。

1 个答案:

答案 0 :(得分:1)

正如斯特凡诺在评论中指出的,install_name_tool可能非常挑剔! 在调用insall_name_tool时,您正在使用类似/../Frameworks/QtCore.framework/Versions/5/QtCore的路径,而引用应为/Users/paul/Qt5.10/etc。 (完全如otool -L所示) 但是使用Qt 5.2使用macdeployqt要容易得多:)