# FindFLEX and CACHE INTERNAL

**URL:** https://discourse.cmake.org/t/findflex-and-cache-internal/9310
**Category:** Usage
**Created:** [October 30, 2023, 3:31pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310 "2023-10-30T15:31:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [October 30, 2023, 3:31pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310/1 "2023-10-30T15:31:31Z")

</div>

I’m having trouble with setting CACHE INTERNAL variables, and must be getting the syntax wrong. The context, in case it’s relevant, is trying to work round a problem with FindFLEX.

I have flex.exe installed in C:\cygwin\bin. On running

```auto
find_package(FLEX ${required_version})

```

… I find that FLEX\_FOUND and FLEX\_EXECUTABLE are set correctly, but FLEX\_INCLUDE\_DIRS is not. That’s irritating, but I can live with it; after all, I know that FlexLexer.h is in C:\cygwin\usr\inlcude, so I say so:

```auto
set(FLEX_INCLUDE_DIRS "C:/cygwin/usr/include" CACHE INTERNAL "")

```

However, CMake seems to completely ignore this, so that if in the very next line I write

```auto
message("FLEX_INCLUDE_DIRS = ${FLEX_INCLUDE_DIRS}")

```

…then the response is:

```auto
FLEX_INCLUDE_DIRS = FLEX_INCLUDE_DIR-NOTFOUND

```

What am I doing wrong?

---

<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: [October 30, 2023, 5:19pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310/2 "2023-10-30T17:19:48Z")

</div>

Scoped variables shadow cache variables, so if `FLEX_INCLUDE_DIRS` is set as a non-cache variable, it will “shadow” the cache setting.

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [October 30, 2023, 6:23pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310/3 "2023-10-30T18:23:00Z")

</div>

So if I understand correctly…

- FindFLEX sets FLEX\_INCLUDE\_DIRS as a non-cache variable;

- I’m then setting it as a cache variable (because I want to use it globally without having to promote it up a couple of levels using PARENT\_SCOPE)

- But when I try to access the variable normally using ${FLEX\_INCLUDE\_DIRS}, I’m seeing the non-cache variable defined by FindFLEX, rather than the cache variable I defined myself?

If so I’m still puzzled, because I can’t see anything in FindFLEX.cmake that raises FLEX\_INCLUDE\_DIRS to the parent scope, and the place where I actually want to invoke it is another level up again.

But am I on the right track?

---

<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: [October 30, 2023, 6:49pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310/4 "2023-10-30T18:49:27Z")

</div>

> [@EosPengwern](#):
>
> FindFLEX sets FLEX\_INCLUDE\_DIRS as a non-cache variable;

Looks like it does.

> [@EosPengwern](#):
>
> I’m then setting it as a cache variable (because I want to use it globally without having to promote it up a couple of levels using PARENT\_SCOPE)

`find_package` is scoped. Unless you set the targets’ `GLOBAL` property, they don’t really do much to parent scopes (except `function` and `macro` definitions…those are always global).

> [@EosPengwern](#):
>
> But when I try to access the variable normally using ${FLEX\_INCLUDE\_DIRS}, I’m seeing the non-cache variable defined by FindFLEX, rather than the cache variable I defined myself?

If you’re in the same scope and after the `find_package` call, yes.

> [@EosPengwern](#):
>
> But am I on the right track?

To understanding, yes. To a fix…I think the `find_package` call just needs to be called in a high enough scope that anything that wants to use it is “after” it and under a relevant directory.

---

<div class="post-metadata">

### Author: ![EosPengwern](https://discourse.cmake.org/user_avatar/discourse.cmake.org/eospengwern/32/3904_2.png) [@EosPengwern](https://discourse.cmake.org/u/EosPengwern)
#### Post date: [November 2, 2023, 9:22pm UTC](https://discourse.cmake.org/t/findflex-and-cache-internal/9310/5 "2023-11-02T21:22:41Z")

</div>

Thank you, yes. The solution was to make sure that find\_package(FLEX) was run in the global namespace, rather than inside a function, and to get rid of all of the cache variables.
