简体   繁体   中英

Qt Libraries Missing on macOS

I am developing a Qt project with C++. However, on Mac, it is impossible to run it, because it can't find any libraries.

I followed Qt website instructions, a Qt file appeared on my home directory.

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. That's what macOS applications actually are; directories that have the ".app" extension. By default, the macOS finder does not show the ".app" extension. The Safari application for example is simply shown as "Safari", not as "Safari.app".

If your project is called "MyApp", the app bundle will have the name "MyApp.app". To make this usable on other machines, you need to use the "macdeployqt" tool. This will copy all needed Qt libraries into the app bundle. You use macdeployqt from the terminal.

Open a terminal and cd into the build directory of your project. You'll find the app bundle of your application there (MyApp.app in this example.) Run macdeployqt like this:

~/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. In this case 5.14.1. This assumes you installed Qt in the default location, which is the "Qt" folder inside your home directory.

The application should now be ready to run on any macOS system. To distribute it easier, you can make a zip out of it by using the "ditto" utility. This tool is part of macOS:

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

This will create MyApp.zip which you can then distribute.

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. This assumes you're using qmake for your project though. Add this to your .pro file:

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:

make macdist

This will do a fresh release build, run macdeployqt on it and package it into a zip.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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