Qt Creator 中依赖文件的自动复制

发布于 2024-11-25 18:47:47 字数 408 浏览 2 评论 0原文

我使用 Qt Creator 2.2.1 和 Qt 4.7.4(32 位)构建了一个程序,其输出是可执行文件。使用 DependencyWalker 打开 exe,它显示该 exe 使用以下 DLL:

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

我想要在构建所有依赖文件后在其他项目中可能会有所不同),但 Windows 特定文件(上面列表中的前两个)会自动复制到所在目录exe 位于。

如何在不使用命令行脚本的情况下在 Qt Creator 或 Qt 系统中完成此操作?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

So尛奶瓶 2024-12-02 18:47:47

在 QT 5.3 中,您也许可以使用 windeployqt qt 工具自动复制所需的库。

在项目的 .pro 文件中添加以下内容应该可以解决问题,但您可能需要根据您的具体情况进行一些调整。

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}

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}
太阳哥哥 2024-12-02 18:47:47

我将修改您的项目的 *.pro 文件并使用 安装。要真正移动文件,您需要运行 make install。在 Qt Creator 中,您可以通过进入“项目”部分并添加新的构建步骤,将其添加为正常构建过程的一部分。

## 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

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
情释 2024-12-02 18:47:47

即使不是完全自动的,您也可以使用 QtCreator 轻松完成您想做的事情。

将“INSTALLS”指令添加到您的项目(.pro)文件中(类似于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

在QtCreator(我的版本= 2.4.1)的“项目”选项卡中,您现在添加一个额外的构建步骤:

  • 点击“添加构建步骤”
  • 选择“制作”
  • 将“覆盖...”保留为空
  • 在“制作参数:”处输入“安装”

由于这些设置不会与项目文件一起保存,因此每次都必须执行第二部分(添加构建步骤)你创建一个新的构建配置(例如,发布和调试各一个)或将您的项目检出到新位置。

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.

亚希 2024-12-02 18:47:47

不不不 0_o :)

  1. 创建一些目录来部署文件,即“bin”。
  2. 在每个 .pro 文件中写入

    DLLDESTDIR = ../../myproject/bin(...某些目录的结构...)

    QMAKE_POST_LINK = Windeployqt --compiler-runtime $$DLLDESTDIR

DLLDESTDIR -- 文件名为 TARGET 的完整路径(dll 和 exe)。

结尾!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文