【问题标题】:CMake: how to use if condition in add_custom_command(...)CMake:如何在 add_custom_command(...) 中使用 if 条件
【发布时间】:2016-04-19 07:27:22
【问题描述】:

我想通过 add_custom_command(...) 在 CMakeLists.txt 中使用 Linux if 条件,因为我需要运行这些 if 条件并在 makefile 中做一些判断。像这样:

cmake_minimum_required(VERSION 2.8)
add_custom_target(temp_target ALL)
add_custom_command(TARGET temp_target
                   PRE_BUILD
                   COMMAND if ["a" != "b"]; then echo 1; fi;
                   VERBATIM )

如果我想使用我应该怎么做

if ["a" != "b"]; then echo 1; fi;

什么时候制作一个makefile? 非常感谢您的帮助!

【问题讨论】:

  • 如果这样不行,为什么不尝试在函数中添加这些,然后在命令中调用该函数?

标签: linux bash cmake makefile cmake-custom-command


【解决方案1】:

您可以使用/bin/sh -c 作为COMMAND 参数来指定一行shell 代码:

COMMAND /bin/sh -c "if [ 'a' != 'b' ]; then echo 1; fi;"

注意,[ 是 bash 的扩展,对于像“dash”这样的简单 shell,它可能是未知的。

【讨论】:

  • 感谢您的回复,但您提供的内容似乎在我的计算机上不起作用。
  • 不要忘记在add_custom_command 中添加VERBATIM !!
【解决方案2】:

您可能需要在分号前加上反斜杠

COMMAND if [ 'a' != 'b' ] \; then echo 1\; fi\;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2012-04-09
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多