CMake:如何在 add_custom_command(...) 中使用 if 条件

CMake: how to use if condition in add_custom_command(...)

我想通过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? 非常感谢您的帮助!

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

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

请注意,[ 是 bash 的扩展,对于像 "dash".

这样的简单 shell,它可能是未知的

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

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