# Where can I find a working example of IF IN\_LIST?

**URL:** <https://discourse.cmake.org/t/where-can-i-find-a-working-example-of-if-in-list/11437>\
**Category:** Code\
**Created:** [August 13, 2024, 4:58pm UTC](https://discourse.cmake.org/t/where-can-i-find-a-working-example-of-if-in-list/11437 "2024-08-13T16:58:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![duaneellis\_inno](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/f04885/32.png) [@duaneellis\_inno](https://discourse.cmake.org/u/duaneellis_inno)\
**Post date:** [August 13, 2024, 4:58pm UTC](https://discourse.cmake.org/t/where-can-i-find-a-working-example-of-if-in-list/11437/1 "2024-08-13T16:58:09Z")

</div>

I am trying to create a test/assert like macro and cannot get the syntax correct  
I am obviusly not writing this macro correctly and I am getting confused/stuck ☹

The macro I have is like this:

```auto
macro( must_be_in_list NEEDLE HAYSTACK )
  if( "${NEEDLE}" IN_LIST HAYSTACK )
    return()
    # all is well
  elseif( ${NEEDLE} STREQUAL ${HAYSTACK} )
    # All is well
    # All is well
  else()
    message( FATAL_ERROR "Needle: ${NEEDLE} not found in ${HAYSTACK}")
  endif()
    
endmacro()

```

I want to use it sort of like this in numerous cases (I am refactoring a large cmake solution) and trying to make some common test cases or asserts to simplify the CMakeLists.txt

```auto
set( n "dog" )   
list( APPEND L "cat" "dog")
must_be_in_list( "${n}" "${L}" )

```

NOTE that sometimes the list is a single word - hence the “STREQUAL” case  
Example:

```auto
    set( n "dog" )
    set( validpets "dog" ) # In this case 
    must_be_in_list( "${n}" "${validpets}" )

```

---

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [August 13, 2024, 5:30pm UTC](https://discourse.cmake.org/t/where-can-i-find-a-working-example-of-if-in-list/11437/2 "2024-08-13T17:30:29Z")

</div>

You want a function, not a macro. Macro arguments are not real CMake variables, and `if(a IN_LIST b)` requires `b` to be the name of a variable.
