14

I've build a program using Qt Creator 2.2.1 and Qt 4.7.4 (32 bit) whose output is an executable. Opening the exe using DependencyWalker it shows that the exe uses following DLLs:

  • KERNEL32.DLL
  • MSVCRT.DLL
  • MINGWM10.DLL
  • LIBGCC_S_DW2-1.DLL
  • QTCORE4.DLL
  • QTGUI4.DLL

I want after the build all dependent files (which may be different in some other project) except Windows specific files (the first two in the above list) to be automatically copied in the directory where the exe is located.

How can I do it in Qt Creator or Qt system without using command line scripting? Thanks.

Donotalo
  • 12,748
  • 25
  • 83
  • 121

4 Answers4

16

In QT 5.3, you may be able to use the windeployqt qt tool to automatically copy the needed libraries.

The following additions to the project's .pro file should do the trick, but you might have to make some adjustments based on your particular situation.

isEmpty(TARGET_EXT) {
    win32 {
        TARGET_CUSTOM_EXT = .exe
    }
    macx {
        TARGET_CUSTOM_EXT = .app
    }
} else {
    TARGET_CUSTOM_EXT = $${TARGET_EXT}
}

win32 {
    DEPLOY_COMMAND = windeployqt
}
macx {
    DEPLOY_COMMAND = macdeployqt
}

CONFIG( debug, debug|release ) {
    # debug
    DEPLOY_TARGET = $$shell_quote($$shell_path($${OUT_PWD}/debug/$${TARGET}$${TARGET_CUSTOM_EXT}))
} else {
    # release
    DEPLOY_TARGET = $$shell_quote($$shell_path($${OUT_PWD}/release/$${TARGET}$${TARGET_CUSTOM_EXT}))
}

#  # Uncomment the following line to help debug the deploy command when running qmake
#  warning($${DEPLOY_COMMAND} $${DEPLOY_TARGET})

# Use += instead of = if you use multiple QMAKE_POST_LINKs
QMAKE_POST_LINK = $${DEPLOY_COMMAND} $${DEPLOY_TARGET}
Scott Marchant
  • 3,447
  • 2
  • 22
  • 29
  • Thanks for letting me know about this feature. :) – Donotalo Aug 09 '14 at 04:55
  • 1
    Be careful when using this. You might be tempted to strip the shell quote/path functionality, but you really don't want to do that. Qt likes to use a more linux-style of slashes. It might make your code look cluttered and ugly, but it's worth the safety boilerplate. – kayleeFrye_onDeck Mar 18 '16 at 20:44
  • Thanks it works. But is there any way to get "make clean" remove the specific files this copies ? I guess I could remove the whole directory of course - just wonder if there is any other approach. – Zitrax Sep 21 '16 at 07:58
  • 1
    Great trick. Please note that this won't work if the ...deployqt program is not in the PATH. In Qt Creator it will be in the path, but it may not be if qmake is run from the shell. Suggestion: `DEPLOY_COMMAND = $$shell_quote($$shell_path($$[QT_INSTALL_BINS]\windeployqt))` and similar for the macdeployqt. – Pat Nov 30 '16 at 13:42
  • I added in my pro file, application not show error but cannot render anything inside windows. – nobjta_9x_tq May 16 '20 at 08:39
5

I would modify your *.pro file for the project and use INSTALLS. To actually cause the files to be moved, you will need to run make install. In Qt Creator, you can add it as part of your normal build process by going into the "Projects" section and adding a new build step.

## This sets MY_LIB_FILES the libs you want and should also correctly resolve
## the location of the libs.

win32 {                ## For Windows builds
    # Note: Check to make sure of file name case

    MY_LIB_FILES += $$QMAKE_LIBDIR_QT/MINGWM10.DLL
    MY_LIB_FILES += $$QMAKE_LIBDIR_QT/LIBGCC_S_DW2-1.DLL
    MY_LIB_FILES += $$QMAKE_LIBDIR_QT/QTCORE4.DLL
    MY_LIB_FILES += $$QMAKE_LIBDIR_QT/QTGUI4.DLL
}

unix {                     ## For unix builds
    # MY_LIB_FILES += $$QMAKE_LIBDIR_QT/...xxxxxx....
}

## Define what files are 'extra_libs' and where to put them
extra_libs.files = MY_LIB_FILES
extra_libs.path = $$DESTDIR

## Tell qmake to add the moving of them to the 'install' target
INSTALLS += extra_libs
jwernerny
  • 6,978
  • 2
  • 31
  • 32
  • I think the complicated part of his question is about to do this **automatically** regarding **dependent files** ... – Thomas Vincent Jul 21 '11 at 15:21
  • I can't make it work. What is `make install` alternative in Windows 7 Ultimate? – Donotalo Jul 23 '11 at 04:02
  • I tried `mingw32-make install` and `qmake install` but neither work. – Donotalo Jul 23 '11 at 04:03
  • @Donotaio You should use the same "make" utility as you do for building your code (or the same one as QtCreator invokes). If you build your code with `nmake`, then use `nmake install`. – jwernerny Jul 23 '11 at 20:44
  • @Donotalo I was also having problem with make install which was just doing nothing. That is because you need to set $$DESTDIR yourself, see this thread : http://stackoverflow.com/a/13168238/62921 – ForceMagic Oct 31 '12 at 22:21
  • One thing i noticed is that the `extra_libs.files = MY_LIB_FILES` line can not work because the variable `MY_LIB_FILES` does not get resolved. To resolve variables double dollar signs ($$) need to be used: `$$MY_LIB_FILES`. So the line becomes: `extra_libs.files = $$MY_LIB_FILES`. Just if someone decides to adopt the answer... – goulashsoup Jul 11 '18 at 21:55
4

Even though not totally automatic, you can with little effort do what you want with QtCreator.

Add the "INSTALLS" directive to your project (.pro) file (similar to what was suggested by jwernerny):

win32 {
    libstocopy.files = $$QMAKE_LIBDIR_QT/MINGWM10.DLL \
       ... (add other files)
}

# If using MSVC the code may end up in "release" or "debug" sub dir
# Remove this if that is not the case
win32 {
    CONFIG(debug, debug|release): OUTDIR = debug
    else: OUTDIR = release
}

libstocopy.path = $$OUT_PWD/$$OUTDIR
INSTALLS += libstocopy

In the "Projects" tab of QtCreator (my version=2.4.1) you now add an extra build step:

  • Hit "Add Build Step"
  • Select "Make"
  • Leave "Override ..." empty
  • Enter "install" at "Make arguments:"

Since these settings are not saved with your project file you have to do the second part (Add Build Step) each time you create a new build configuration (e.g. one each for release and debug) or check out your project to a new location.

jmidgren
  • 141
  • 3
-1

No-no-no 0_o :)

  1. Create some directory to deploy files, i.e. "bin".
  2. In every .pro file write

    DLLDESTDIR = ../../myproject/bin (... some dir's struct ...)

    QMAKE_POST_LINK = windeployqt --compiler-runtime $$DLLDESTDIR

DLLDESTDIR -- full path with file name TARGET (dll and exe).

End!