gpt4 book ai didi

c++ - CMake MacOS X bundle with BundleUtiliies for Qt application

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:40:02 25 4
gpt4 key购买 nike

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

// 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 可执行文件,OK .

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

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 文件夹中,应用程序能够运行并迁移到另一个系统。

它部分解决了问题,但我想用 CMake 和 BundleUtilities 填充 bundle 并且没有 macdeployqt 工具。

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

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

提前致谢。

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

最佳答案

将下面的代码添加到 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. 打印 APPSQT_PLUGINSDIRS 是可选的,但非常有用。
  5. 应该仅手动复制/安装那些未从应用中引用的动态库(包括插件)。 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)

关于c++ - CMake MacOS X bundle with BundleUtiliies for Qt application,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35612687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com