# Can I create a global variable to use for future projects?

**URL:** https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359
**Category:** Usage
**Created:** [June 9, 2020, 5:07pm UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359 "2020-06-09T17:07:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![johnratius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johnratius/32/412_2.png) [@johnratius](https://discourse.cmake.org/u/johnratius)
#### Post date: [June 9, 2020, 5:07pm UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/1 "2020-06-09T17:07:21Z")

</div>

I am wondering if there is a way to set a “global” variable, except it can be used with other projects that don’t initially declare it.  
Let me explain: I have a folder on my system that contains many useful include folders and files for many of my C++ projects. I would like to create a global variable that CMake can access, such as `set(GLOGAL_INCLUDES_FOLDER)`. That way, when I start a CMake project, I can just call `include_directories(${GLOGAL_INCLUDES_FOLDER})`, and it will contain all my include directories I need.

Can I do this in CMake?

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [June 9, 2020, 5:30pm UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/2 "2020-06-09T17:30:24Z")

</div>

CMake can dereference environment variables. You could define an environment variable named `GLOCAL_INCLUDES_FOLDER`, and then use it in a CMakeList like this:

```
include_directories($ENV{GLOCAL_INCLUDES_FOLDER})

```

If the variable is intended for CMake use only, prefer to use CMake-style directory separators (forward slash) in it.

---

<div class="post-metadata">

### Author: ![johnratius](https://discourse.cmake.org/user_avatar/discourse.cmake.org/johnratius/32/412_2.png) [@johnratius](https://discourse.cmake.org/u/johnratius)
#### Post date: [June 9, 2020, 7:19pm UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/3 "2020-06-09T19:19:14Z")

</div>

@Angew Yes, I am intending to use it with CMake only. Where would I declare this, so all CMake files have access to it? Would I need to configure it in the CMake GUI application?

---

<div class="post-metadata">

### Author: ![kyle.edwards](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/k/65b543/32.png) [@kyle.edwards](https://discourse.cmake.org/u/kyle.edwards)
#### Post date: [June 9, 2020, 7:29pm UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/4 "2020-06-09T19:29:17Z")

</div>

Changing project code to rely on variables set in a file on the user’s computer isn’t a good idea, because then the project isn’t portable (which may not be a concern if it’s only for your own use.) Setting [`CMAKE_SYSROOT`](https://cmake.org/cmake/help/latest/variable/CMAKE_SYSROOT.html) to a sysroot that includes your include directory may be a better idea.

If you’re willing to add a little bit of extra code to the project, you can do:

```cmake
include(/path/to/settings/file.cmake)

```

If you REALLY don’t want to add extra code to the project, you could modify one of the CMake system module files, but I would **STRONGLY** recommend not doing that. Your settings would be overwritten any time CMake gets updated.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [June 10, 2020, 6:28am UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/5 "2020-06-10T06:28:38Z")

</div>

> [@johnratius](#):
>
> Yes, I am intending to use it with CMake only. Where would I declare this, so all CMake files have access to it? Would I need to configure it in the CMake GUI application?

You already have a solution, but just to answer your questions. I was talking about creating a system environment variable (similar to `PATH`). That is what CMake dereferences using the `$ENV{var_name}` syntax. The way to declare this depends on your operating system.

---

<div class="post-metadata">

### Author: ![Grant\_Anderson](https://discourse.cmake.org/user_avatar/discourse.cmake.org/grant_anderson/32/498_2.png) [@Grant\_Anderson](https://discourse.cmake.org/u/Grant_Anderson)
#### Post date: [June 12, 2020, 1:55am UTC](https://discourse.cmake.org/t/can-i-create-a-global-variable-to-use-for-future-projects/1359/6 "2020-06-12T01:55:29Z")

</div>

To: Kyle Edwards, Petr Kmoch, and others  
Re: Scope of Global Variables

When the CMake Generator (cmake -G “Eclipse CDT4 - Unix Makefiles”) process the following command found in CMakeLists.txt:

```
include_directories($(MACRO_UTILS_INC_FOLDER))

```

a generated readme.md file states the following:

```
"The project will export a global variable MACRO_UTILS_INC_FOLDER that points to the folder needed to be included so that `#include "azure_macro_utils/macro_utils.h"` resolves properly."

```

but any such “global variable” disappears once CMake execution has finished.

so, with nothing in the Generated makefile to “include directories”, how can the generated “makefile” ever work? And it does not work.

This looks like a big Generator Error to me, unless somebody has a solution.

Thanks,  
Garry Anderson.  
2020-06-11
