Skip to content

Instantly share code, notes, and snippets.

@SebastianMosiej
Last active October 2, 2025 11:00
Show Gist options
  • Select an option

  • Save SebastianMosiej/ed0c8066c2bf2afa98c7cf1110859240 to your computer and use it in GitHub Desktop.

Select an option

Save SebastianMosiej/ed0c8066c2bf2afa98c7cf1110859240 to your computer and use it in GitHub Desktop.
CMake function to deploy Qt library after binary build
# Add folder with this file by SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
# Add include(windeployqt)
# Then windeployqt(target)
if (WIN32)
find_package(Qt6Core REQUIRED)
# get absolute path to qmake, then use it to find windeployqt executable
get_target_property(_qmake_executable Qt6::qmake IMPORTED_LOCATION)
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
endif()
OPTION(USE_WINDEPLOYQT "After successful build deploy to build folder needed Qt libraries" OFF)
function(windeployqt target)
if (USE_WINDEPLOYQT AND WIN32)
# POST_BUILD step
# - after build, we have a bin/lib for analyzing qt dependencies
# - we run windeployqt on target and deploy Qt libs
add_custom_command(TARGET ${target} POST_BUILD
COMMAND "${_qt_bin_dir}/windeployqt.exe"
--verbose 1
--no-compiler-runtime
--no-system-d3d-compiler
--qmldir ${CMAKE_SOURCE_DIR}
\"$<TARGET_FILE:${target}>\"
COMMENT "Deploying Qt libraries using windeployqt for compilation target '${target}' ..."
)
cmake_path(GET CMAKE_CXX_COMPILER PARENT_PATH QT_MINGW)
cmake_path(GET QT_MINGW PARENT_PATH QT_MINGW)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_MINGW}/bin/libc++.dll \"$<TARGET_FILE_DIR:${target}>\"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_MINGW}/bin/libunwind.dll \"$<TARGET_FILE_DIR:${target}>\"
COMMENT "Deploy mingw runtime libraries from ${QT_MINGW}/bin"
)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_custom_command(TARGET ${target} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_MINGW}/bin/libgcc_s_seh-1.dll \"$<TARGET_FILE_DIR:${target}>\"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_MINGW}/bin/libstdc++-6.dll \"$<TARGET_FILE_DIR:${target}>\"
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${QT_MINGW}/bin/libwinpthread-1.dll \"$<TARGET_FILE_DIR:${target}>\"
COMMENT "Deploy mingw runtime libraries from ${QT_MINGW}/bin"
)
endif()
endif()
endfunction()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment