Ole*_*zan 6 c++ macos qt bundle cmake
我是一名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();
}
Run Code Online (Sandbox Code Playgroud)
该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 )
Run Code Online (Sandbox Code Playgroud)
我跑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
Run Code Online (Sandbox Code Playgroud)
据我所知,错误发生是因为应用程序包没有任何Qt内容(Framework库和插件),所以我运行macdeployqt它并在Framework和PlugIns文件夹中填充很多文件的bundle目录,并且应用程序能够运行并重新安置到另一个系统.
它部分地解决了这个问题,但我想用CMake和BundleUtilities填充bundle 而不用macdeployqt工具.
不幸的是,我没有找到任何关于使用BundleUtilities进行Qt5部署的简单示例.
有人可以帮我修改我的'helloworld'示例,以便CMake自动创建可随时部署的捆绑包吗?
提前致谢.
将下面的代码添加到CMakeLists.txt. 最具挑战性的事情是弄清楚您需要哪些插件,找到它们的名称,然后正确指定 BundleUtilities' 的路径fixup_bundle()。
install_qt5_plugin()宏按名称定位插件。它只会找到已经找到的 Qt 模块的插件。在这种情况下,Qt5::QCcoaIntegrationPlugin 是 Qt5Gui 模块中的插件,它被发现为 Qt5Widgets 的依赖项find_package(Qt5 COMPONENTS Widgets REQUIRED)。宏为插件生成 install() 命令并计算已安装插件的完整路径。后者我们将传递(见QT_PLUGIN变量)到fixup_bundle().
笔记:
qt.conf文件,以便在应用程序启动时找到插件。APPS 变量指定包的路径,而不是其中的可执行文件。DIRS非常重要。请注意,它如何使用 CMAKE_PREFIX_PATH。APPS,QT_PLUGINS并且DIRS是可选的但非常有用。依赖项查找和修复发生在安装时。要在必要的位置获得可重定位的包,可以使用指向该位置的 CMAKE_INSTALL_PREFIX 进行配置,然后构建install目标。
我更喜欢创建 .dmg 文件
mkdir build
cd build
cmake ..
cpack -G DragNDrop
Run Code Online (Sandbox Code Playgroud)
添加到 CMakeLists.txt 的内容来自这里:
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)
Run Code Online (Sandbox Code Playgroud)