如何在Windows上打包使用CMake构建的Qt程序?

时间:2019-11-22 04:53:06

标签: windows qt cmake package

我正在编写一个跨平台程序,希望将其打包在Windows上。我通过在构建目录中放置三个DLL(Qt5Core,Qt5Widgets,Qt5Gui)来运行它,然后制作了一个tarball,并用Windows盒子将其提供给其他人,但是它不能在他的Windows盒子上运行。我搜索了一种获取所需其他信息的方法,并找到了内森·奥斯曼(Nathan Osman)关于windeployqt的帖子,因此我将windeployqt.cmake复制到cmake / Modules目录,但不知道如何使用它。是否可以使用CPack制作一个程序包(即使只是一个tarball),该程序包具有在未安装编译器或Qt的Windows机器上运行程序所需的一切?

这是CMakeLists.txt文件,其中删除了一些(希望是不相关的)内容:

project(perfecttin)
cmake_minimum_required(VERSION 3.8.0)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD 17) # appeared in CMake 3.8
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(SHARE_DIR ${CMAKE_INSTALL_PREFIX}/share/perfecttin)
add_definitions(-DBOOST_ALL_NO_LIB)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(Boost_USE_STATIC_LIBS ON)
else ()
set(Boost_USE_STATIC_LIBS OFF)
endif ()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
find_package(LibPLYXX)
find_package(Qt5 COMPONENTS Core Widgets Gui LinguistTools REQUIRED)
find_package(Boost COMPONENTS program_options)
find_package(Threads)

qt5_add_resources(lib_resources perfecttin.qrc)
qt5_add_translation(qm_files perfecttin_en.ts perfecttin_es.ts)
# To update translations, run "lupdate *.cpp -ts *.ts" in the source directory.

add_executable(perfecttin-gui adjelev.cpp angle.cpp binio.cpp
           <snip lots of source files>
               unitbutton.cpp ${lib_resources} ${qm_files})
target_link_libraries(perfecttin-gui ${CMAKE_THREAD_LIBS_INIT} Qt5::Widgets Qt5::Core)
target_link_libraries(fuzzptin ${CMAKE_THREAD_LIBS_INIT})
target_compile_definitions(fuzzptin PUBLIC _USE_MATH_DEFINES)
target_compile_definitions(perfecttin-gui PUBLIC _USE_MATH_DEFINES)
set_target_properties(perfecttin-gui PROPERTIES WIN32_EXECUTABLE TRUE)

install(TARGETS perfecttin-gui DESTINATION bin)
install(FILES ${qm_files} DESTINATION share/perfecttin)

include_directories(${PROJECT_BINARY_DIR})
configure_file (config.h.in config.h)

set(CPACK_PACKAGE_VERSION_MAJOR ${PERFECTTIN_MAJOR_VERSION})
set(CPACK_PACKAGE_VERSION_MINOR ${PERFECTTIN_MINOR_VERSION})
set(CPACK_PACKAGE_VERSION_PATCH ${PERFECTTIN_PATCH_VERSION})
set(CPACK_RESOURCE_FILE_LICENSE ${CMAKE_SOURCE_DIR}/COPYING)
set(CPACK_SOURCE_IGNORE_FILES /\\\\.git;.*~)
include(CPack)

include(CTest)

2 个答案:

答案 0 :(得分:1)

如果${CMAKE_SOURCE_DIR}/cmake/Modules/目录中已经有文件Windeployqt.cmake,则可以这样使用它:

install(TARGETS perfecttin-gui 
    DESTINATION "${INSTALL_BIN_PATH}"
)

if(WIN32)
    include(Windeployqt)
    windeployqt(nitroshare-cli ${INSTALL_BIN_PATH})
endif()

Here是完整的示例。

答案 1 :(得分:0)

我在这方面花了很长时间,虽然我从 this answer 那里借了很多东西,但是它缺少导致 Warning: Cannot find GCC installation directory. g++.exe must be in the path. 的必要环境设置。

Qt 有以下关于使用 windeployqt.exe 的文档

The tool can be found in QTDIR/bin/windeployqt. It needs to be run within the build environment in order to function correctly.
When using Qt Installer, the script QTDIR/bin/qtenv2.bat should be used to set it up.

基本上归结为使用正确的环境设置将 windeployqt.exe 作为自定义命令运行。

# Retrieve the absolute path to qmake and then use that path to find
# the windeployqt executable
get_target_property(QMAKE_EXE Qt6::qmake IMPORTED_LOCATION)
get_filename_component(QT_BIN_DIR"${QMAKE_EXE }" DIRECTORY)

find_program(WINDEPLOYQT_ENV_SETUP qtenv2.bat HINTS "${QT_BIN_DIR}")
find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${QT_BIN_DIR}")

# Run windeployqt immediately after build
add_custom_command(TARGET perfecttin-gui
    POST_BUILD
    COMMAND "${WINDEPLOYQT_ENV_SETUP}" && "${WINDEPLOYQT_EXECUTABLE}" \"$<TARGET_FILE:perfectin-gui>\"
)