Jam*_*ton 30 c++ qt qmake qt4 qt-creator
我有一个在Qt Creator中创建的简单项目(使用Qt SDK 1.1.4安装).它在Qt Creator中运行得很好,但如果我然后浏览到Windows中的输出目录并双击EXE,我将收到如下错误:
The program can't start because QtCored4.dll is missing from your computer.
Try reinstalling the program to fix this problem.
这显然是因为Qt不在我的PATH中(我不希望它,如果我的计算机上有多个版本的Qt),Qt Creator/qmake没有将Qt DLL复制到项目输出.
我想要做的是使用qmake将必要的Qt文件复制到项目输出目录 - 无论它在哪里.我该怎么做呢?
(我尝试在qmake中创建一个自定义目标,但我不会太过分......)
更新2016年7月19日:   为了澄清,上述帖子涉及Qt4.在Qt5上,你应该考虑调用windeployqt.此Qt5工具将读取您的二进制文件,确定您需要哪些Qt5运行时文件,并将它们复制到二进制目录.另请注意,它将修复Qt5 :: Core库中特定于PC的绝对路径 - 因此,除非您想自己提供qt.conf文件,否则使用此工具基本上是强制性的.
好的,这是一个丑陋的黑客:
# Copy required DLLs to output directory
CONFIG(debug, debug|release) {
    QtCored4.commands = copy /Y %QTDIR%\\bin\\QtCored4.dll debug
    QtCored4.target = debug/QtCored4.dll
    QtGuid4.commands = copy /Y %QTDIR%\\bin\\QtGuid4.dll debug
    QtGuid4.target = debug/QtGuid4.dll
    QMAKE_EXTRA_TARGETS += QtCored4 QtGuid4
    PRE_TARGETDEPS += debug/QtCored4.dll debug/QtGuid4.dll
} else:CONFIG(release, debug|release) {
    QtCore4.commands = copy /Y %QTDIR%\\bin\\QtCore4.dll release
    QtCore4.target = release/QtCore4.dll
    QtGui4.commands = copy /Y %QTDIR%\\bin\\QtGui4.dll release
    QtGui4.target = release/QtGui4.dll
    QMAKE_EXTRA_TARGETS += QtCore4 QtGui4
    PRE_TARGETDEPS += release/QtCore4.dll release/QtGui4.dll
} else {
    error(Unknown set of dependencies.)
}
以下是我不喜欢的一些内容:
我会接受另一个答案,如果有人可以清理这一点,例如缩短它和/或解决我的一些问题,或者只是找到更好的方法.
有点干净的方法,但它需要做一个make install后make.它可以在Windows上运行,但需要调整其他平台.
debug { DESTDIR = debug }
release { DESTDIR = release }
debug_and_release { DESTDIR = bin }
myqtlibs.path = $$DESTDIR
myqtlibs.files = $$QMAKE_LIBDIR_QT/*.dll junk.txt fred.out
myqtlibs.CONFIG = no_check_exist
INSTALLS += myqtlibs
如果仅为调试运行qmake,则所有输出都将进入./debug.如果只是为了发布,所有输出都进入./release.如果两者都进入./bin.
我注意到在QtCreator中启用阴影构建导致可执行文件不会在DESTDIR中结束.我不太清楚为什么.
使用windeployqt复制依赖项
# Deployment - Automatically Detect and Copy Dependencies to Build Folder
TARGET_CUSTOM_EXT = .exe
DEPLOY_COMMAND = windeployqt
DEPLOY_OPTIONS = "--no-svg --no-system-d3d-compiler --no-opengl --no-angle --no-opengl-sw"
CONFIG( debug, debug|release ) {
    # debug
    DEPLOY_TARGET = $$shell_quote($$shell_path($${OUT_PWD}/debug/$${TARGET}$${TARGET_CUSTOM_EXT}))
    DEPLOY_OPTIONS += "--debug"
} else {
    # release
    DEPLOY_TARGET = $$shell_quote($$shell_path($${OUT_PWD}/release/$${TARGET}$${TARGET_CUSTOM_EXT}))
    DEPLOY_OPTIONS += "--release"
}
# Uncomment the following line to help debug the deploy command when running qmake
#message($${DEPLOY_COMMAND} $${DEPLOY_OPTIONS} $${DEPLOY_TARGET})
QMAKE_POST_LINK = $${DEPLOY_COMMAND} $${DEPLOY_OPTIONS} $${DEPLOY_TARGET}
或手动复制依赖项
# Deployment - Copy Dependencies to Build Folder
dlls.path  =  $${DESTDIR}
dlls.files += $$[QT_INSTALL_BINS]/icudt51.dll
dlls.files += $$[QT_INSTALL_BINS]/icuin51.dll
dlls.files += $$[QT_INSTALL_BINS]/icuuc51.dll
dlls.files += $$[QT_INSTALL_BINS]/libgcc_s_dw2-1.dll
dlls.files += $$[QT_INSTALL_BINS]/libstdc++-6.dll
dlls.files += $$[QT_INSTALL_BINS]/libwinpthread-1.dll
dlls.files += $$[QT_INSTALL_BINS]/Qt5Core.dll
dlls.files += $$[QT_INSTALL_BINS]/Qt5Network.dll
dlls.files += $$[QT_INSTALL_BINS]/Qt5Gui.dll
dlls.files += $$[QT_INSTALL_BINS]/Qt5Widgets.dll
dllA.path   += $${DESTDIR}/platforms
dllA.files  += $$[QT_INSTALL_PLUGINS]/platforms/qwindows.dll
dllB.path   += $${DESTDIR}/plugins/imageformats/
dllB.files  += $$[QT_INSTALL_PLUGINS]/imageformats/qico.dll
dllB.files  += $$[QT_INSTALL_PLUGINS]/imageformats/qwbmp.dll
INSTALLS   += dlls dllA dllB
参考:http://doc.qt.io/qt-5/qmake-variable-reference.html#deployment
如果您需要跨平台识别先决条件/依赖项,请查看CMake getPrerequisites().它使用dumpbin,objbin,ldd,otool为相关性的鉴定.
参考:https://cmake.org/cmake/help/v3.0/module/GetPrerequisites.html