qmake命令将文件和文件夹复制到输出目录中

时间:2014-06-29 09:54:27

标签: windows qt qmake xcopy

我正在开发一个应该使用QtCreator和Qt 5.3在Windows,Linux和OS X上构建的应用程序。我想将文件夹中的所有文件和子文件夹复制到输出文件夹中。我已经将它用于Linux和OS X,但不适用于Windows。这是我的.pro文件的相关部分:

win32 {
    PWD_WIN = $${PWD}
    DESTDIR_WIN = $${OUT_PWD}
    copyfiles.commands = $$quote(cmd /c xcopy /S /I $${PWD_WIN}\copy_to_output $${DESTDIR_WIN})
}
macx {
    copyfiles.commands = cp -r $$PWD/copy_to_output/* $$OUT_PWD
}
linux {
    copyfiles.commands = cp -r $$PWD/copy_to_output/* $$OUT_PWD
}
QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

我在Windows上遇到的错误是“参数数量无效”。

2 个答案:

答案 0 :(得分:10)

如果您使用$${PWD}查看message($${PWD})变量,您会看到/作为目录分隔符,即使在Windows中也是如此。您必须将其转换为本机目录分隔符:

PWD_WIN = $${PWD}
DESTDIR_WIN = $${OUT_PWD}
PWD_WIN ~= s,/,\\,g
DESTDIR_WIN ~= s,/,\\,g

copyfiles.commands = $$quote(cmd /c xcopy /S /I $${PWD_WIN}\\copy_to_output $${DESTDIR_WIN})

QMAKE_EXTRA_TARGETS += copyfiles
POST_TARGETDEPS += copyfiles

答案 1 :(得分:2)

建立@Murat's answer,Qt实际上has built-in functions将文件路径转换为本地系统的首选项。

$$shell_path( <your path> ) //Converts to OS path divider preference.
$$clean_path( <your path> ) //Removes duplicate dividers.

称之为:$$shell_path($$clean_path( <your path> )),或$$clean_path()将分隔符转换回Linux风格的分隔符。

这适用于Windows:

#For our copy command, we neeed to fix the filepaths to use Windows-style path dividers.
SHADER_SOURCE_PATH = $$shell_path($$clean_path("$${SOURCE_ROOT}\\Engine\\Shaders\\"))
SHADER_DESTINATION = $$shell_path($$clean_path("$${PROJECT_BIN}\\Shaders\\"))

#Create a command, using the 'cmd' command line and Window's 'xcopy', to copy our shaders folder
#into the Game/Bin/Shaders/ directory.
CopyShaders.commands = $$quote(cmd /c xcopy /Y /S /I $${SHADER_SOURCE_PATH} $${SHADER_DESTINATION})

#Add the command to Qt.
QMAKE_EXTRA_TARGETS += CopyShaders
POST_TARGETDEPS += CopyShaders