如何复制Qt运行时DLL到项目输出 [英] How to copy Qt runtime DLLs to project output

查看:853
本文介绍了如何复制Qt运行时DLL到项目输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的项目创建在Qt Creator(使用Qt SDK 1.1.4安装)。它运行正常从内部Qt Creator,但如果我然后浏览到Windows中的输出目录,并双击EXE,我会得到一个错误,如:

I have a simple project created in Qt Creator (installed using Qt SDK 1.1.4). It runs just fine from within Qt Creator, but if I then browse to the output directory in Windows and double-click the EXE, I'll get an error like:

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复制到项目输出。

That's obviously because Qt isn't in my PATH (and I don't want it to be, in case I have multiple versions of Qt on my computer), and Qt Creator / qmake didn't copy the Qt DLLs to the project output.

做是使用qmake将必要的Qt文件复制到项目输出目录 - 无论它在哪里。

What I would like to do is use qmake to copy the necessary Qt files to the project output directory - wherever it may be. How do I do this?

(我尝试在qmake中创建一个自定义目标,但我没有太远...)

(I tried creating a custom target in qmake, but I'm not getting too far...)

更新2016年7月19日:为了澄清,上面的帖子是关于Qt4的。在Qt5上,您应该查看调用 windeployqt 。这个Qt5工具将读取您的二进制文件,确定您需要哪些Qt5运行时文件,并将其复制到您的二进制目录。还要注意,它将修复特定于您的PC的Qt5 :: Core库中的绝对路径 - 因此,使用这个工具基本上是强制性的,除非你想提供 qt.conf

UPDATE July 19, 2016: Just to clarify, the above post was concerning Qt4. On Qt5, you should instead look into calling windeployqt. This Qt5 tool will read your binary, determine which Qt5 runtime files you need, and copy them to your binary directory. Also note that it will fix absolute paths in the Qt5::Core library that are specific to your PC - so use of this tool is basically mandatory unless you want to provide a qt.conf file yourself.

推荐答案

确定,这是一个丑陋的黑客:

OK, here's an ugly hack:

# 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.)
}

这里是我不喜欢的一些:

Here's some of what I don't like about it:


  • 使用%QTDIR%环境变量;在实际运行copy命令之前不会评估此变量。看起来像是 QMAKE_LIBS_QT_DLL 的行为更合适,但我无法得到那种工作的原因。

  • 硬编码的调试和释放名称;


  • Uses %QTDIR% environment variable; this variable isn't evaluated until the copy command is actually run. Seems like something along the lines of QMAKE_LIBS_QT_DLL would be more appropriate, but I couldn't get that working for some reason.
  • Hard-coded "debug" and "release" names; seems like there ought to be some kind of variable to use for that.
  • Calling out to the environment by using the "copy" command.

我会接受另一个答案,如果有人可以清理这一点,例如通过缩短和/或解决我的一些问题,或者只是找到一个更好的方法。

I'll accept another answer if somebody can clean this up a good bit, for example by shortening it and/or addressing some of my concerns, or just finding a better way in general.

这篇关于如何复制Qt运行时DLL到项目输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆