Automatic Copy of Dependent Files in Qt Creator

一笑奈何 提交于 2019-11-28 20:46:07

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}

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

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.

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!