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

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:

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


…then the response is:


What am I doing wrong?

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

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?

Looks like it does.

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).

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

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.

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.