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

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?

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.

@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?

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 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:

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.

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.

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