首页 > 解决方案 > 在命令行中设置 cmake 变量并将其与字符串进行比较

问题描述

我这样称呼我的 CMakeLists.txt:

cmake   ../.. -DTARGET=JETSON_NANO

然后,这一行:

message(STATUS "------------ TARGET: ${TARGET}")

印刷------------ TARGET: JETSON_NANO

但是这一行:

if (TARGET STREQUAL JETSON_NANO)

给出错误:

if given arguments:

    "TARGET" "STREQUAL" "JETSON_NANO"

为什么?TARGET定了!

标签: cmake

解决方案


TARGET是命令的特殊关键字if。它用于检查给定目标(在 CMake 意义上)是否存在。此关键字的正确用法包括以下两个参数if

if(TARGET JETSON_NANO) # Checks whether CMake target JETSON_NANO exists

这就是为什么当你使用这个关键字和三个参数时 CMake 会发出错误:

if (TARGET STREQUAL "JETSON_NANO") # Error: 'TARGET' keyword requires two `if` arguments

但是,您可以在命令中交换比较的字符串:if

if ("JETSON_NANO" STREQUAL TARGET) # Compares string "JETSON_NANO" with variable TARGET

在其文档中查看有关if命令的更多信息。


推荐阅读