使用 CMake CPack NSIS 打包 Qt 应用


if(WIN32 AND NOT UNIX)
    get_target_property(qmake_executable Qt6::qmake IMPORTED_LOCATION)
    get_filename_component(_qt_bin_dir "${qmake_executable}" DIRECTORY)
    find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
    message(STATUS "Using windeployqt: ${WINDEPLOYQT_EXECUTABLE}")
    message(STATUS "qt bin dir: ${_qt_bin_dir}")
    add_custom_command(TARGET ${target_name} POST_BUILD
        COMMAND "${WINDEPLOYQT_EXECUTABLE}"
        $<$<CONFIG:Debug>:--debug>
        $<$<NOT:$<CONFIG:Debug>>:--release>
        --libdir $<TARGET_FILE_DIR:${target_name}>
        --verbose 0
        --compiler-runtime
        --no-opengl-sw
        --force
        --plugindir "$<TARGET_FILE_DIR:${target_name}>/plugins"
        $<TARGET_FILE:${target_name}>
        COMMENT "Deploying Qt..."
    )

    if(QT_VERSION_MAJOR EQUAL 6)
        qt_finalize_executable(${target_name})
    endif()

    include(GNUInstallDirs)
    install(PROGRAMS
        $<TARGET_FILE:${target_name}>
        DESTINATION . # TYPE BIN
    )

    install(DIRECTORY
        $<TARGET_FILE_DIR:${target_name}>/
        DESTINATION .
        FILES_MATCHING PATTERN "*.dll"
    )

    # CPack
    # InstallRequiredSystemLibraries 以及选项
    # https://cmake.org/cmake/help/latest/module/InstallRequiredSystemLibraries.html
    set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION TRUE)
    set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
    set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION ".")
    include(InstallRequiredSystemLibraries)

    set(CPACK_GENERATOR "IFW")
    set(CPACK_IFW_ROOT "D:/dev_libs/Qt/Tools/QtInstallerFramework/4.10")
    set(CPACK_PACKAGE_VERSION_MAJOR "1")
    set(CPACK_PACKAGE_VERSION_MINOR "0")

    # set human names to exetuables
    set(CPACK_PACKAGE_EXECUTABLES "${PROJECT_NAME}" "MAVLink Monitor installer")
    set(CPACK_CREATE_DESKTOP_LINKS "${PROJECT_NAME}")
endif()

include(CPACK)
include(CPackIFW)

编译完成之后,生成CPackConfig.cmake,在build目录下执行:

cpack.exe ./CPackConfig.cmake

参考




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • al-folio 本地部署记录(Ubuntu 24.04)
  • C++ Traits
  • 道格拉斯-普克算法(Douglas–Peucker algorithm)
  • CMake支持库收集
  • QGC代码架构解析:飞行前检查(起飞条件)