使用BundleUtiliies CMake MacOS X捆绑用于Qt应用程序

问题描述 投票:6回答:1

我是一名CMake初学者,并且在为MacOS X创建Qt应用程序包时遇到了问题。让我们在一个main.cpp文件中考虑一个简单的小部件“helloworld”应用程序。

// main.cpp
#include <QApplication>
#include <QLabel>

int main(int argc, char** argv)
{
    QApplication app(argc,argv);
    QLabel lbl("Hello");
    lbl.show();
    return app.exec();
}

CMakeLists.txt文件也很简单。

# CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( QtBundle )    
set( CMAKE_INCLUDE_CURRENT_DIR ON )
set( CMAKE_AUTOMOC ON )

set( SOURCES main.cpp )    
find_package( Qt5Widgets REQUIRED )

add_executable( ${PROJECT_NAME} MACOSX_BUNDLE ${SOURCES} )    
qt5_use_modules( ${PROJECT_NAME} Widgets )

我运行cmake .. -DCMAKE_PREFIX_PATH=/path/to/Qt5.5.1/,它在Makefile目录中生成build

然后我运行make并拥有我想要的QtBundle.app目录和QtBundle.app/Contents/MacOS/QtBundle可执行文件,好的。

但是当我启动它时,我得到:

This application failed to start because it could not find or load the Qt platform plugin "cocoa".

Reinstalling the application may fix this problem.
Abort trap: 6 

据我所知,错误发生是因为应用程序包没有任何Qt内容(框架库和插件),所以我运行macdeployqt,它用Framework和PlugIns文件夹中的很多文件填充bundle目录,应用程序能够运行并重新定位到另一个系统。

它部分解决了这个问题,但我想用CMake和BundleUtilities填充bundle而不用macdeployqt工具。

不幸的是,我没有找到任何关于使用BundleUtilities进行Qt5部署的好的简单示例。

有人可以帮我修改我的'helloworld'示例,以便CMake自动创建可随时部署的捆绑包吗?

提前致谢。

Main question: how to use CMake BundleUtilities to get a relocatable application?

c++ macos qt cmake bundle
1个回答
3
投票

将以下代码添加到CMakeLists.txt。最具挑战性的是弄明白,你需要什么插件,找到他们的名字,然后为BundleUtilities的fixup_bundle()正确指定路径。

install_qt5_plugin()宏按名称定位插件。它只会找到Qt模块的插件。在这种情况下,Qt5 :: QCocoaIntegrationPlugin是Qt5Gui模块中的插件,它被find_package(Qt5 COMPONENTS Widgets REQUIRED)发现为Qt5Widgets的依赖项。 Macro为插件生成install()命令并计算已安装插件的完整路径。后者我们将通过(见QT_PLUGIN变量)到fixup_bundle()

笔记:

  1. 我们创建并安装qt.conf文件,因此可以在应用程序启动时找到插件。
  2. APPS变量指定bundle的路径,而不是其中的可执行文件。
  3. 填充DIRS非常重要。注意,它如何使用CMAKE_PREFIX_PATH。
  4. 打印APPSQT_PLUGINSDIRS是可选的,但非常有用。
  5. 应该手动复制/安装那些未从app引用的动态库(包括插件)。 Qt平台插件就是这样的动态库。

安装时会发生依赖关系查找和修复。要在可能的位置获取可重定位的bundle,可以使用指向该位置的CMAKE_INSTALL_PREFIX进行配置,然后构建install目标。

我更喜欢创建.dmg文件

mkdir build
cd build
cmake ..
cpack -G DragNDrop

要添加到CMakeLists.txt的内容来自here

set(prefix "${PROJECT_NAME}.app/Contents")
set(INSTALL_RUNTIME_DIR "${prefix}/MacOS")
set(INSTALL_CMAKE_DIR "${prefix}/Resources")

# based on code from CMake's QtDialog/CMakeLists.txt
macro(install_qt5_plugin _qt_plugin_name _qt_plugins_var _prefix)
    get_target_property(_qt_plugin_path "${_qt_plugin_name}" LOCATION)
    if(EXISTS "${_qt_plugin_path}")
        get_filename_component(_qt_plugin_file "${_qt_plugin_path}" NAME)
        get_filename_component(_qt_plugin_type "${_qt_plugin_path}" PATH)
        get_filename_component(_qt_plugin_type "${_qt_plugin_type}" NAME)
        set(_qt_plugin_dest "${_prefix}/PlugIns/${_qt_plugin_type}")
        install(FILES "${_qt_plugin_path}"
            DESTINATION "${_qt_plugin_dest}")
        set(${_qt_plugins_var}
            "${${_qt_plugins_var}};\$ENV{DEST_DIR}\${CMAKE_INSTALL_PREFIX}/${_qt_plugin_dest}/${_qt_plugin_file}")
    else()
        message(FATAL_ERROR "QT plugin ${_qt_plugin_name} not found")
    endif()
endmacro()

install_qt5_plugin("Qt5::QCocoaIntegrationPlugin" QT_PLUGINS ${prefix})
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
    "[Paths]\nPlugins = ${_qt_plugin_dir}\n")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/qt.conf"
    DESTINATION "${INSTALL_CMAKE_DIR}")

# Destination paths below are relative to ${CMAKE_INSTALL_PREFIX}
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION . COMPONENT Runtime
    RUNTIME DESTINATION ${INSTALL_RUNTIME_DIR} COMPONENT Runtime
    )

# Note Mac specific extension .app
set(APPS "\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/${PROJECT_NAME}.app")

# Directories to look for dependencies
set(DIRS "${CMAKE_BINARY_DIR}")

# Path used for searching by FIND_XXX(), with appropriate suffixes added
if(CMAKE_PREFIX_PATH)
    foreach(dir ${CMAKE_PREFIX_PATH})
        list(APPEND DIRS "${dir}/bin" "${dir}/lib")
    endforeach()
endif()

# Append Qt's lib folder which is two levels above Qt5Widgets_DIR
list(APPEND DIRS "${Qt5Widgets_DIR}/../..")

include(InstallRequiredSystemLibraries)

message(STATUS "APPS: ${APPS}")
message(STATUS "QT_PLUGINS: ${QT_PLUGINS}")
message(STATUS "DIRS: ${DIRS}")

install(CODE "include(BundleUtilities)
    fixup_bundle(\"${APPS}\" \"${QT_PLUGINS}\" \"${DIRS}\")")

set(CPACK_GENERATOR "DRAGNDROP")
include(CPack)
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.