【问题标题】:How to run multiple commands with an extra target in QMake如何在 QMake 中运行带有额外目标的多个命令
【发布时间】:2013-08-05 14:00:25
【问题描述】:

我正在使用qmake 创建额外的目标,并且我正在尝试同时做两件事:创建一个新文件夹,然后将一个 dll 复制到该文件夹​​中。两个动作分开工作正常,但两者一起不起作用。

something.target = this

# This works:
# something.commands =   mkdir newFolder
# This works too (if newFolder exists)
# something.commands =   copy /Y someFolder\\file.dll newFolder

# This doesn't work:
something.commands = mkdir newFolder; \
                     copy /Y someFolder\\file.dll newFolder

QMAKE_EXTRA_TARGETS += something
PRE_TARGETDEPS += this

我认为这是正确的语法(我发现了类似的示例,例如 herehere),但我收到以下错误:

> mkdir newFolder; copy /Y someFolder\\file.dll newFolder
> The syntax of the command is incorrect.

不同平台上的语法是否不同?我正在使用 Qt 5.0.1 在 Windows 7 上工作。

【问题讨论】:

    标签: qt qmake


    【解决方案1】:

    .commands 变量的值被 qmake 原样粘贴到 Makefile 中目标命令的位置。 qmake 从值中去除任何空格并将它们更改为单个空格,因此如果没有特殊工具就不可能创建多行值。还有工具:函数 escape_expand。试试这个:

    something.commands = mkdir newFolder $$escape_expand(\n\t) copy /Y someFolder\\file.dll newFolder
    

    $$escape_expand(\n\t) 添加换行符(结束上一个命令)并按照 Makefile 语法规定使用制表符开始下一个命令。

    【讨论】:

    • 太棒了,就像一个魅力!但出于好奇:是否有其他线程似乎使用; \ 作为换行符没有问题的原因。这个平台依赖吗?
    • 它可能是一种特定于 shell 的功能,用于将多个命令合并为一个。例如,在 Windows 上 && 将与标准 cmd shell 一起使用。我不太了解类 unix 的 shell,无法确定地回答。
    • @SergeySkoblikov:在 Unix 下,&& 应该也能正常工作 -> 它在第一个命令之后运行第二个命令,但前提是第一个命令成功完成。无论如何,只有& 会运行第二个命令。 windows下好像也一样?
    【解决方案2】:

    and 运算符在 Linux 上也适用于我,奇怪的是 windows。

    something.commands = mkdir newFolder && copy /Y someFolder\\file.dll newFolder
    

    【讨论】:

      【解决方案3】:

      如果您想避免反斜杠,也可以附加到 .commands 变量:

      target.commands += mkdir toto
      target.commands += && copy ...
      # Result will be:
      target:
          mkdir toto && copy ...
      

      或者:

      target.commands += mkdir toto;
      target.commands += copy ...;
      # Result will be:
      target:
          mkdir toto; copy ...;
      

      【讨论】:

        猜你喜欢
        • 2016-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-03
        • 2013-06-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多