Poperton
Poperton

Reputation: 1796

Setting cmake variable in command line and comparing it with string

I'm calling my CMakeLists.txt like this:

cmake   ../.. -DTARGET=JETSON_NANO

Then, this line:

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

prints ------------ TARGET: JETSON_NANO

but this line:

if (TARGET STREQUAL JETSON_NANO)

gives error:

if given arguments:

    "TARGET" "STREQUAL" "JETSON_NANO"

Why? TARGET is setted!

Upvotes: 0

Views: 747

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66288

TARGET is a special keyword for if command. It is used for check whether given target (in CMake sense) exists. Correct usage of this keyword includes two arguments of if:

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

This is why CMake emits error when you use this keyword with three arguments:

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

However, you may swap compared strings in if command:

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

See more about if command in its documentation.

Upvotes: 3

Related Questions