How to copy Qt runtime DLLs to project output

后端 未结 4 1307
生来不讨喜
生来不讨喜 2021-02-01 15:46

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

4条回答
  •  广开言路
    2021-02-01 16:14

    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:

    • 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.

提交回复
热议问题