listcmakecomparisonoperatorsmembership

When using IN_LIST in a CMake condition, getting "Unknown arguments specified"


Consider the following CMakeLists.txt file:

cmake_minimum_required(VERSION 3.25.0 FATAL_ERROR)
project(foo)
set(my_list one two three)
if ("two" IN_LIST ${my_list})
  message("two is in your list")
else()
  message("two is NOT in your list")
endif()

When I run this (i.e. when cmake-configuring using this file), I get:

CMake Error at CMakeLists.txt:4 (if):
  if given arguments:

    "two" "IN_LIST" "one" "two" "three"

  Unknown arguments specified

What's wrong with my script? I thought this is how IN_LIST should work....


Solution

  • The right parameter of IN_LIST condition is a name of the variable, which contains a list. Not a content of the list.

    Correct:

    set(my_list one two three)
    if ("two" IN_LIST my_list)
      message("two is in your list")
    else()
      message("two is NOT in your list")
    endif()