# How to know the scope of variables

**URL:** https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683
**Category:** Usage
**Created:** [April 17, 2024, 8:39pm UTC](https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683 "2024-04-17T20:39:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dabrahams](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dabrahams/32/4265_2.png) [@dabrahams](https://discourse.cmake.org/u/dabrahams)
#### Post date: [April 17, 2024, 8:39pm UTC](https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683/1 "2024-04-17T20:39:35Z")

</div>

When I see a variable like `CMAKE_ARCHIVE_OUTPUT_DIRECTORY` being set I always wonder whether it affects just the current project or directory, or whether it is truly global, and I’m not sure how to tell other than by running experiments. I realize there are `PROJECT_…` and `CURRENT_…` variables for which the answers are clear based on both names and documentation. Is that pattern upheld consistently?

Thanks in advance

---

<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: [April 27, 2024, 3:35pm UTC](https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683/2 "2024-04-27T15:35:22Z")

</div>

You can use `get_property(CACHE varname)` to query the cache directly. If the `VALUE` property doesn’t match the current state, you have a local variable shadowing the cache.

---

<div class="post-metadata">

### Author: ![dabrahams](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dabrahams/32/4265_2.png) [@dabrahams](https://discourse.cmake.org/u/dabrahams)
#### Post date: [April 29, 2024, 7:09pm UTC](https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683/3 "2024-04-29T19:09:45Z")

</div>

I think you explained how to know whether the variable is in the cache, but that’s not what I asked—though my question probably reflects a misunderstanding.

I thought some variables I might set in a CMakeLists.txt file are global and could affect dependent projects, some apply to a project and could affect ancestor or sibling directories in the same project, and some only affect subdirectories. Now that I write it down it sounds wrong…

I think the cmake-language manual clears this up (and I know I’ve read this before): variables are dynamically scoped subdirectory scopes are nested. No need to reply if I’ve got that right, thanks.

---

<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: [April 29, 2024, 8:49pm UTC](https://discourse.cmake.org/t/how-to-know-the-scope-of-variables/10683/4 "2024-04-29T20:49:54Z")

</div>

Ah! No variables are “special” to the cache or local scope. Which one any given documented variable is tends to be down to convention. There are some that CMake looks at that only matter in the top-level scope (e.g., why `include(CTest)` must be done there). _Those_ should be documented as such (and we’d need to survey which ones these are).
