# Generator expression not evaluated

**URL:** https://discourse.cmake.org/t/generator-expression-not-evaluated/8264
**Category:** Usage
**Created:** [June 6, 2023, 12:06pm UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264 "2023-06-06T12:06:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Panu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@Panu](https://discourse.cmake.org/u/Panu)
#### Post date: [June 6, 2023, 12:06pm UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264/1 "2023-06-06T12:06:38Z")

</div>

Is this supposed to work:

```auto
cmake_minimum_required(VERSION 3.25)
 
project(TEST)
 
add_executable(test_ext dummy.cpp)
add_library(lib_a STATIC dummy.cpp)
add_library(lib_b STATIC dummy.cpp)
 
target_link_libraries(lib_a
    PUBLIC
        $<1:lib_b>
)
 
target_link_libraries(test_ext
    PUBLIC
        $<TARGET_PROPERTY:lib_a,LINK_LIBRARIES>
)

```

Running commands:

```auto
touch dummy.cpp
cmake -S . -B out -G Ninja
cmake --build out

```

Fails with:

```auto
ninja: error: build.ninja:63: bad $-escape (literal $ must be written as $$)
   LINK_LIBRARIES = $<1:lib_b>
                    ^ near here

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 7, 2023, 1:45pm UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264/2 "2023-06-07T13:45:40Z")

</div>

It looks like `test_ext`’s link line isn’t recursively expanding genexes; `lib_a` is fine. Full stanza:

```plaintext
build test_ext: CXX_EXECUTABLE_LINKER__test_ext_ CMakeFiles/test_ext.dir/dummy.cpp.o
  LINK_LIBRARIES = $<1:lib_b>
  OBJECT_DIR = CMakeFiles/test_ext.dir
  POST_BUILD = :
  PRE_LINK = :
  TARGET_COMPILE_PDB = CMakeFiles/test_ext.dir/
  TARGET_FILE = test_ext
  TARGET_PDB = test_ext.pdb

```

Cc: @brad.king

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [June 7, 2023, 2:46pm UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264/3 "2023-06-07T14:46:51Z")

</div>

> [@Panu](#):
>
> Is this supposed to work:

No.

> [@ben.boeckel](#):
>
> isn’t recursively expanding genexes

It isn’t supposed to. See the [`GENEX_EVAL`](https://cmake.org/cmake/help/v3.26/manual/cmake-generator-expressions.7.html#multi-level-expression-evaluation) expression.

> [@Panu](#):
>
> ```auto
> target_link_libraries(test_ext
> PUBLIC
> $<TARGET_PROPERTY:lib_a,LINK_LIBRARIES>
> )
> 
> ```

This is not typical usage. The `LINK_LIBRARIES` of a target are meant to be private. Use just

```cmake
target_link_libraries(test_ext PUBLIC lib_a)

```

This looks like a [x-y problem](https://xyproblem.info/). Please describe what you are really trying to do, perhaps in a new topic dedicated to your use case rather than the genex error.

---

<div class="post-metadata">

### Author: ![Panu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@Panu](https://discourse.cmake.org/u/Panu)
#### Post date: [June 7, 2023, 5:12pm UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264/4 "2023-06-07T17:12:51Z")

</div>

Thanks for the information. Even after reading the linked documentation it’s not easy to understand how generator expressions are evaluated.

I discovered this problem by adding:

```auto
target_link_libraries(lib_a
    PUBLIC
        $<1:lib_b>
)

```

It took me a while to realise that the build failed because someone had used in a completely different place (scope was actually PRIVATE and not PUBLIC as in my first message):

```auto
target_link_libraries(test_ext
    PRIVATE
        $<TARGET_PROPERTY:lib_a,LINK_LIBRARIES>
)

```

I agree that this is not typical usage and I don’t understand why it was used in this case. But there it was and it made the build fail. Luckily I can modify that part of the code too and remove this redundant generator expression. It would be nice if CMake could detect this kind of errors and print a useful error message instead of creating invalid build.ninja file.

---

<div class="post-metadata">

### Author: ![Panu](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e95f7d/32.png) [@Panu](https://discourse.cmake.org/u/Panu)
#### Post date: [June 8, 2023, 8:32am UTC](https://discourse.cmake.org/t/generator-expression-not-evaluated/8264/5 "2023-06-08T08:32:42Z")

</div>

Part of the confusion is that in this example all generator expressions are evaluated:

```auto
cmake_minimum_required(VERSION 3.25)

project(TEST)

add_executable(test_ext dummy.cpp)
add_library(lib_a STATIC dummy.cpp)
add_library(lib_b STATIC dummy.cpp)

target_include_directories(lib_a
    PUBLIC
        $<1:${CMAKE_CURRENT_LIST_DIR}/foo_dir>
)

target_include_directories(test_ext
    PRIVATE
        $<TARGET_PROPERTY:lib_a,INTERFACE_INCLUDE_DIRECTORIES>
)

```

After running commands:

```auto
touch dummy.cpp
mkdir foo_dir
cmake -S . -B out -G Ninja

```

There’s this in build.ninja:

```auto
build CMakeFiles/test_ext.dir/dummy.cpp.o: CXX_COMPILER__test_ext_ /host/workdir/tmp/cmake/gen_exp_inc/dummy.cpp || cmake_object_order_depends_target_test_ext
  DEP_FILE = CMakeFiles/test_ext.dir/dummy.cpp.o.d
  INCLUDES = -I/host/workdir/tmp/cmake/gen_exp_inc/foo_dir
  OBJECT_DIR = CMakeFiles/test_ext.dir
  OBJECT_FILE_DIR = CMakeFiles/test_ext.dir
  TARGET_COMPILE_PDB = CMakeFiles/test_ext.dir/
  TARGET_PDB = test_ext.pdb

```

There’s foo\_dir in INCLUDES as one expects. It’s not easy to understand why generator expressions are evaluated here but not in the first example.
