【问题标题】:CMake MacOS X bundle with BundleUtiliies for Qt applicationCMake MacOS X 与 BundleUtiliies 捆绑用于 Qt 应用程序
【发布时间】:2016-06-07 08:45:12
【问题描述】:

我是一名 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/ 并在build 目录中生成Makefile。

然后我运行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 并使用框架和插件文件夹中的大量文件填充包目录并且应用程序能够运行并重新定位到另一个系统。

它部分解决了问题,但我想用 CMake 和 BundleUtilities 填充捆绑包,并且不使用 macdeployqt 工具。

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

有人可以帮助我修改我的“helloworld”示例,使 CMake 自动创建准备部署的包吗?

提前致谢。

主要问题:如何使用 CMake BundleUtilities 获取可重定位应用程序?

【问题讨论】:

  • 我也在寻找答案。你有办法解决这个问题吗?我发现的所有东西要么来自上一个时代,要么描述得不好并且没有显示出来。
  • @light2yellow 不,我不使用 BundleUtilities,而是使用 macdeployqt 并在安装步骤中使用 cmake 复制必要的文件,并使用 Packages 制作包。它有效。
  • 无论如何我找到了一个很好的例子。看看 avogadro 或其他套件项目。

标签: c++ macos qt cmake bundle


【解决方案1】:

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

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

注意事项:

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

依赖项查找和修复发生在安装时。要在必要的位置获得可重定位的捆绑包,可以使用指向该位置的 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 CMakes 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)

【讨论】:

  • 这似乎仍然会为${PROJECT_NAME}.app/Contents/PlugIns/platforms/libqcocoa.dylib' 产生常见错误Install or copy the item into the bundle before calling fixup_bundle.。
  • 由于_qt_plugin_dir没有定义,qt.conf中的路径是空的,是不是故意的?
猜你喜欢
  • 1970-01-01
  • 2017-02-28
  • 2020-07-05
  • 1970-01-01
  • 2014-10-08
  • 2021-12-24
  • 2017-02-11
  • 1970-01-01
  • 2011-10-08
相关资源
最近更新 更多