# CMake Generator Expression IN\_LIST

**URL:** <https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145>\
**Category:** Code\
**Created:** [May 4, 2020, 9:54pm UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145 "2020-05-04T21:54:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 4, 2020, 9:54pm UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145/1 "2020-05-04T21:54:09Z")

</div>

I have the following CMake setup which is not including the sources as expected. (`PLATFORM` is set to by a toolchain file, note I’m not using CMAKE\_SYSTEM\_NAME because one of my platforms (not listed below) isn’t supported by that convention). Note I’m including the multiple variations below since none seem to work for me.

```auto
target_sources(myLib
    PRIVATE
        $<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
        $<IN_LIST:${PLATFORM},"Qnx;Linux">:platform-source.cpp>
        # oddly enough this works for target_sources but not for target_link_libraries
        $<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>
)

```

What am I missing here?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [May 5, 2020, 7:53am UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145/2 "2020-05-05T07:53:16Z")

</div>

`$<IN_LIST:...> is a boolean genex, so it returns `0`or`1`. You have to encapsulate this genex in the logical operator ` `$<BOOL:string>`. The `$<` is missing at the beginning of the expression:

```cmake
target_sources(myLib
    PRIVATE
        $<$<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
)

```

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 5, 2020, 1:05pm UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145/3 "2020-05-05T13:05:29Z")

</div>

Sorry that was a typo in my post ☹  
I do have

```auto
target_sources(myLib
    PRIVATE
        $<$<IN_LIST:${PLATFORM},"Qnx Linux">:platform-source.cpp>
        $<$<IN_LIST:${PLATFORM},"Qnx;Linux">:platform-source.cpp>
        # oddly enough this works for target_sources but not for target_link_libraries
        $<$<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>
)

```

Which still doesn’t work as expected.

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [May 5, 2020, 1:20pm UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145/4 "2020-05-05T13:20:47Z")

</div>

A basic rule for genex is to encapsulate a genex in quotes.

From your example, the first item is wrong because `"Qnx Linux"` is not a list. Nor the second because it is encapsulated in quotes.

The third syntax is OK except that, because the genex is not in quotes, it will be expanded during `target_sources` evaluation and your arguments will be divided in two items: `$<$<IN_LIST:${PLATFORM},Qnx` and `Linux>:platform-source.cpp>`

The correct syntax is:

```cmake
target_sources(myLib
    PRIVATE
        "$<$<IN_LIST:${PLATFORM},Qnx;Linux>:platform-source.cpp>"
)

```

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 5, 2020, 3:36pm UTC](https://discourse.cmake.org/t/cmake-generator-expression-in-list/1145/5 "2020-05-05T15:36:59Z")

</div>

That worked, thank you!

It would be helpful if that was mentioned in the documentation ([https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html)). Their examples with `target_compile_definitions` do not put the generator expression in quotes.
