與 CMake 整合

可以從 CMake 執行 windeployqtmacdeployqt,但首先必須找到可執行檔案的路徑:

# Retrieve the absolute path to qmake and then use that path to find
# the binaries
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
find_program(WINDEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
find_program(MACDEPLOYQT_EXECUTABLE macdeployqt HINTS "${_qt_bin_dir}")

為了讓 windeployqt 在其安裝位置找到 Qt 庫,必須將該資料夾新增到%PATH%。要在構建之後為名為 myapp 的目標執行此操作:

add_custom_command(TARGET myapp POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E
        env PATH="${_qt_bin_dir}" "${WINDEPLOYQT_EXECUTABLE}"
            "$<TARGET_FILE:myapp>"
    COMMENT "Running windeployqt..."
)

對於在繫結上執行 macdeployqt,它將以這種方式完成:

add_custom_command(TARGET myapp POST_BUILD
    COMMAND "${MACDEPLOYQT_EXECUTABLE}"
        "$<TARGET_FILE_DIR:myapp>/../.."
        -always-overwrite
    COMMENT "Running macdeployqt..."
)