Qt项目打包需要使用 windeployqt.exe这个程序,故想要在cmake中也要想方法调用windeployqt.exe。
- 首秀找到qmake.exe所在路径
# 获得qmake所在路径(Qt5.15.2/5.15.2/msvc2019_64/bin/qmake.exe)
get_target_property(qmake_exec_filepath Qt5::qmake IMPORTED_LOCATION)
# qt bin所在目录 (Qt5.15.2/5.15.2/msvc2019_64/bin)
get_filename_component(qt_exec_bin_dir "${qmake_exec_filepath}" DIRECTORY)
2.找到windeployqt.exe所在路径
# windeployqt所在路径(Qt5.15.2/5.15.2/msvc2019_64/bin/windeployqt.exe)
find_program(windeployqt_exec_filepath windeployqt HINTS "${qt_exec_bin_dir}")
3.为了让 windeployqt 在其安装位置找到 Qt 库,必须将qmake.exe所在路径添加到 %PATH%。要在构建后对名为 myapp(可以直接用 ${PROJECT_NAME} ) 的目标执行此操作
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E
env PATH="${qt_exec_bin_dir}" "${windeployqt_exec_filepath}"
"$<TARGET_FILE:${PROJECT_NAME}>"
COMMENT "Running windeployqt..."
)
构建完成后会自动拷贝Qt的动态库之类的到目标目录(项目生成的 xxxx.exe )。
只针对Windows