# Generator Expressions and Namespace in target\_link\_libraries

**URL:** <https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038>\
**Category:** Code\
**Created:** [March 29, 2021, 12:45pm UTC](https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038 "2021-03-29T12:45:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [March 29, 2021, 12:45pm UTC](https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038/1 "2021-03-29T12:45:02Z")

</div>

Yet another post in “Matt tries to learn Effective CMake”. This time, it’s generator expressions!

So, I have a code where we want to link in a library only if an `option()` is true. So I have (simplified):

```cmake
target_link_libraries(this PRIVATE MPI::MPI_Fortran)
if (BUILD_WITH_FLAP)
  target_link_libraries(this PRIVATE FLAP::FLAP)
endif ()

```

and this works. But then I thought “Maybe I can make this more compact (and much less readable?) with Generator Expressions!” 🙂 So I tried:

```cmake
target_link_libraries(this PRIVATE MPI::MPI_Fortran $<$<BOOL:BUILD_WITH_FLAP>:FLAP::FLAP>)

```

Now, obviously, if this worked, I wouldn’t be here. When I try this I get:

```auto
CMake Error at ESMA_cmake/ecbuild/cmake/ecbuild_add_library.cmake:268 (add_library):
  Target "MAPL.cap" links to target "FLAP::FLAP" but the target was not
  found. Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

```

which is a fancy enough CMake error that I’m not sure what it means.

So, what is the right way to do this? Or should I just stick with the more-readable three-line if-test version?

ETA: Note: We do have in the code:

```cmake
option(BUILD_WITH_FLAP "Use FLAP for command line processing" ON)
if (BUILD_WITH_FLAP)
  find_package(FLAP REQUIRED)
endif ()

```

and I am building with `-DBUILD_WITH_FLAP=OFF`

---

<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:** [March 29, 2021, 12:53pm UTC](https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038/2 "2021-03-29T12:53:56Z")

</div>

> [@mathomp4](#):
>
> `target_link_libraries(this PRIVATE MPI::MPI_Fortran $<$<BOOL:BUILD_WITH_FLAP>:FLAP::FLAP>)`

This should be:

```cmake
target_link_libraries(this PRIVATE MPI::MPI_Fortran $<$<BOOL:${BUILD_WITH_FLAP}>:FLAP::FLAP>)

```

This is because variables don’t “exist” at genex evaluation time, so `BUILD_WITH_FLAP` is taken as a string for which to determine the boolean-ness of. Since it is not one of CMake’s “false” values, it is “true”.

---

<div class="post-metadata">

**Author:** ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)\
**Post date:** [March 29, 2021, 1:01pm UTC](https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038/3 "2021-03-29T13:01:40Z")

</div>

Graaaah! I think I got hit with this in one of my other questions here or on GitHub Actions or something. Thanks, it of course works. Now to ponder if I should stick with easy-to-read-but-verbose, or compact-but-preserves-my-job-because-complicated. 😃

---

<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:** [March 29, 2021, 1:03pm UTC](https://discourse.cmake.org/t/generator-expressions-and-namespace-in-target-link-libraries/3038/4 "2021-03-29T13:03:42Z")

</div>

For complicated expressions, I recommend [building them up using explicit variable names](https://gitlab.kitware.com/vtk/vtk/-/blob/master/Utilities/Python/CMakeLists.txt#L150).
