简体   繁体   English

macOS 上缺少 Qt 库

[英]Qt Libraries Missing on macOS

I am developing a Qt project with C++.我正在用 C++ 开发一个 Qt 项目。 However, on Mac, it is impossible to run it, because it can't find any libraries.但是,在 Mac 上,无法运行它,因为它找不到任何库。

I followed Qt website instructions, a Qt file appeared on my home directory.我按照 Qt 网站的说明进行操作,我的主目录中出现了一个 Qt 文件。

drwxr-xr-x  14 Toan  staff     448  2 mar 20:25 .
drwxr-xr-x+ 52 Toan  staff    1664  2 mar 20:21 ..
-rw-r--r--@  1 Toan  staff    8196  2 mar 21:23 .DS_Store
-rw-r--r--   1 Toan  staff  195548 16 fév 23:22 InstallationLog.txt
drwxr-xr-x   5 Toan  staff     160 16 fév 23:13 Licenses
drwxrwxrwx   3 Toan  staff      96 16 fév 23:13 MaintenanceTool.app
-rw-r--r--   1 Toan  staff  276471 16 fév 23:13 MaintenanceTool.dat
-rw-r--r--   1 Toan  staff    6274 16 fév 23:13 MaintenanceTool.ini
drwxr-xr-x   3 Toan  staff      96 16 fév 23:13 Qt Creator.app
-rw-r--r--   1 Toan  staff    2794 16 fév 23:13 components.xml
drwxr-xr-x   3 Toan  staff      96 16 fév 23:13 dist
drwxrwxrwx   7 Toan  staff     224  2 mar 21:23 installerResources
-rw-r--r--   1 Toan  staff     362 16 fév 23:13 network.xml
-rw-r--r--   1 Toan  staff  151525  5 fév 11:55 update.rcc

When you build your project, a so called "app bundle" is created.构建项目时,会创建一个所谓的“应用程序包”。 It's a directory with the .app extension.它是一个带有.app扩展名的目录。 That's what macOS applications actually are;这就是 macOS 应用程序的实际情况; directories that have the ".app" extension.具有“.app”扩展名的目录。 By default, the macOS finder does not show the ".app" extension.默认情况下,macOS 查找器不显示“.app”扩展名。 The Safari application for example is simply shown as "Safari", not as "Safari.app".例如,Safari 应用程序仅显示为“Safari”,而不是“Safari.app”。

If your project is called "MyApp", the app bundle will have the name "MyApp.app".如果您的项目名为“MyApp”,则应用程序包将具有名称“MyApp.app”。 To make this usable on other machines, you need to use the "macdeployqt" tool.要使其在其他机器上可用,您需要使用“macdeployqt”工具。 This will copy all needed Qt libraries into the app bundle.这会将所有需要的 Qt 库复制到应用程序包中。 You use macdeployqt from the terminal.您从终端使用 macdeployqt。

Open a terminal and cd into the build directory of your project.打开终端并cd进入项目的构建目录。 You'll find the app bundle of your application there (MyApp.app in this example.) Run macdeployqt like this:你会在那里找到你的应用程序的应用程序包(在这个例子中是 MyApp.app。)像这样运行 macdeployqt:

~/Qt/5.14.1/clang/bin/macdeployqt MyApp.app

Remember to use the macdeployqt tool that is shipped with the version of Qt you built your project with.请记住使用随您构建项目的 Qt 版本一起提供的 macdeployqt 工具。 In this case 5.14.1.在这种情况下 5.14.1。 This assumes you installed Qt in the default location, which is the "Qt" folder inside your home directory.这假设您将 Qt 安装在默认位置,即主目录中的“Qt”文件夹。

The application should now be ready to run on any macOS system.该应用程序现在应该可以在任何 macOS 系统上运行了。 To distribute it easier, you can make a zip out of it by using the "ditto" utility.为了更轻松地分发它,您可以使用“同上”实用程序制作一个 zip 文件。 This tool is part of macOS:此工具是 macOS 的一部分:

ditto -v -c -k --sequesterRsrc --keepParent --zlibCompressionLevel 9 MyApp.app MyApp.zip

This will create MyApp.zip which you can then distribute.这将创建 MyApp.zip,然后您可以分发它。

Doing this by hand every time however is a bit annoying.然而,每次手动执行此操作有点烦人。 What I do is create a custom make target in my project file that does this for me.我所做的是在我的项目文件中创建一个自定义 make 目标,为我执行此操作。 This assumes you're using qmake for your project though.这假设您正在为您的项目使用 qmake。 Add this to your .pro file:将此添加到您的 .pro 文件中:

macx {
    macdist.target = macdist
    macdist.commands = \
        rm -rf "$${TARGET}.app" \
        && rm -f "$${TARGET}.zip" \
        && "$$QMAKE_QMAKE" -config release "$$_PRO_FILE_" \
        && make clean \
        && make -j$$QMAKE_HOST.cpu_count \
        && "$$dirname(QMAKE_QMAKE)/macdeployqt" "$${TARGET}.app" \
        && ditto -v -c -k --sequesterRsrc --keepParent --zlibCompressionLevel 9 "$${TARGET}.app" "$${TARGET}.zip"

    QMAKE_EXTRA_TARGETS += macdist
}

Now all you need to do is run qmake as usual (in Creator select "Build->Run qmake" in the menu) and then in the terminal go into the build directory and just do:现在您需要做的就是像往常一样运行 qmake(在 Creator 中选择菜单中的“Build->Run qmake”),然后在终端中进入构建目录,然后执行以下操作:

make macdist

This will do a fresh release build, run macdeployqt on it and package it into a zip.这将进行全新的发布构建,在其上运行 macdeployqt 并将其打包成 zip。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM