如何使用 CPack 惯用地打包 QT 应用程序的依赖项? [英] How to idiomatically package dependencies for a QT application using CPack?
问题描述
我有一个简单的 QT 项目.我正在 Linux 上开发.但它最终将部署到 Linux、Mac 和 Windows.
I have a simple QT project. I'm developing on Linux. But it's meant to be deployed to Linux, Mac and Windows ultimately.
我正在尝试将其打包以进行分发.我在定位依赖项并将它们打包并以惯用方式执行此操作时遇到问题(IOW:没有硬编码的 DLL 路径或在我的源代码库中包含 DLL)
I'm attempting to package it for distribution. I'm running into problems locating the dependencies and packaging them up and doing this in an idiomatic way (IOW: No hardcoded paths to DLLs or including the DLLs in my source repo)
对于 Windows 端口,我使用 MinGW 并像这样编译:
For the Windows port, I'm using MinGW and compiling like this:
mingw64-cmake -G "Unix Makefiles" .. -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=/usr/share/mingw/toolchain-mingw64.cmake
make && ctest && make install && cpack -G "TGZ" && cpack -G "NSIS64"
我已将其设置为生成 tar.gz
文件和 NSIS
安装程序.目前没有特别的理由需要 NSIS 而不是 Wix.这只是为了弄清楚.
I've set it to product a tar.gz
file and an NSIS
installer. There's no particular reason for NSIS and not Wix at the moment. This is just to figure things out.
它编译 Windows 可执行文件,但不包含运行程序所需的 DLL.就是这些:
It compiles a Windows executable, but it does not include the DLLs necessary to run the program. It's these:
Libgcc_s_seh-1.dll
Qt5Core.dll
Qt5Gui.dll
在我的计算机上快速查找显示这些 DLL 位于此处:
A quick find on my computer shows me that those DLLs are present here:
/usr/x86_64-w64-mingw32/sys-root/mingw/bin/Qt5Widgets.dll
/usr/x86_64-w64-mingw32/sys-root/mingw/bin/libgcc_s_seh-1.dll
...
有没有办法自动让 CPack 挖掘 DLL 并将它们包含在安装程序中?
这是我的 CMakeLists.txt
文件:
cmake_minimum_required(VERSION 2.8.11)
project(myapp)
enable_testing()
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
add_executable(myapp WIN32 main.cpp mainwindow.cpp resources.qrc)
target_link_libraries(myapp Qt5::Widgets)
target_link_libraries(myapp Qt5::Core)
target_link_libraries(myapp Qt5::Gui)
INSTALL(TARGETS myapp
BUNDLE DESTINATION .
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)
INSTALL(FILES ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT Libraries)
IF(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
INSTALL(PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS} DESTINATION bin COMPONENT System)
ENDIF(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS)
INCLUDE(CPack)
我四处寻找这方面的帮助.我遇到的最好的事情是此链接
I've looked around for some help on this. The best thing I came across was this link
但它看起来并不那么地道.如果我们仔细查看 CMakeLists.txt 文件,它有特定于机器的硬编码路径,将来肯定会改变:
But it's not looking so idiomatic. If we look closer at the CMakeLists.txt file, it's got machine-specific hard coded paths that are certain to change in the future:
IF( WIN32 AND ${ARCH_32BIT})
SET(QT_INSTALLED_PATH "C:/QtMSVCX86/Qt5.5.0/5.5/msvc2013" )
ELSEIF(WIN32 AND ${ARCH_64BIT})
SET(QT_INSTALLED_PATH "C:/QtMSVCX64/Qt5.5.0/5.5/msvc2013_64" )
ELSEIF(UNIX AND NOT MINGW AND ${ARCH_32BIT})
SET(QT_INSTALLED_PATH "/opt/Qt5.5.0/5.5/gcc/" )
ELSEIF(UNIX AND NOT MINGW AND ${ARCH_64BIT})
SET(QT_INSTALLED_PATH "/opt/Qt5.5.0/5.5/gcc_64/" )
ENDIF()
SET(CMAKE_AUTOMOC ON)
SET(CMAKE_AUTOUIC ON)
SET(CMAKE_AUTORCC ON)
FIND_PACKAGE(Qt5Widgets PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH)
FIND_PACKAGE(Qt5Qml PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH)
FIND_PACKAGE(Qt5Quick PATHS ${QT_INSTALLED_PATH} NO_DEFAULT_PATH)
推荐答案
看看 CMake 的 Bundle Utilities 它包含一个 FIXUP_BUNDLE 宏,用于收集可执行文件的所有必要依赖项,包括 Qt.它在 Windows、Linux 和 Mac 上的工作方式基本相同.您可以首先将 FIXUP_BUNDLE(myapp)
添加到您的 CMake 文件中.实际的依赖解析和复制发生在 CPack 运行期间.根据项目大小和复杂性,需要进行一些调整,但总的来说,我已经看到它成功地用于具有基于 CMake 的构建系统的大型跨平台 Qt 项目
Have a look at CMake's Bundle Utilities It contains a FIXUP_BUNDLE macro which collects all the necessary dependencies of an executable, including Qt. It works basically the same way on Windows, Linux and Mac. You could start by adding FIXUP_BUNDLE(myapp)
to your CMake file. the actual dependency resolving and copying happens during the CPack run. Depending on project size and complexity, some tweaks are necessary, but in general I have seen it used successfully in larger cross-platform Qt projects with a CMake based build system
这篇关于如何使用 CPack 惯用地打包 QT 应用程序的依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!