# Specifying system-specific environment variables

**URL:** <https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546>\
**Category:** Code\
**Created:** [December 3, 2023, 3:16pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546 "2023-12-03T15:16:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ciobi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ciobi/32/3859_2.png) [@Ciobi](https://discourse.cmake.org/u/Ciobi)\
**Post date:** [December 3, 2023, 3:16pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/1 "2023-12-03T15:16:21Z")

</div>

Hi,

This looks like a very basic question, but I was unable to find a good solution searching this site and others, or asking ChatGPT or Bard. So …

I have a QMake-based project for Linux and Windows that I’m trying to move to CMake. While Linux is OK, Windows (Visual Studio 2022) not really. In a sense it works, as I can build; but it isn’t “right”. Inside CMakeLists.txt I need several environment variables pointing to the location of libraries and tools. All is fine if I hardcode them or ask the user to pass them, but I want neither of these. To me, they should be in an external file, set up once and never mentioned again until the path of those tools changes. For this I found 3 potential solutions, but I couldn’t make any work:

1. Use _CMakeUserPresets.json_. The thing is, I don’t know what to put there. I was hoping that I can put the _windows-base_ preset (I’m adapting a project VS generated) with just the _environment_ entry, while _CMakePresets.json_ would also have the _windows-base_ preset, but will have the other entries and the fields would get merged. Alas, what I get is a “duplicate presets” error.
2. Use _include_ in _CMakePresets.json_. This is pretty similar to _CMakeUserPresets.json_, in that I get a “duplicate presets” error.
3. Use some other custom file and tell _CMakePresets.json_ to load data from it. This is something ChatGPT came up with, recommending _“$\<1:$LOADS:my\_environment.json\>”_. I can’t figure out if this is complete nonsense or it needs a little tweak, but CMake only seems to treat it as a literal string.

I already have a `.cmd` file that computes the values that I need, I’m just not sure what to do with them, given that I want to be able to both build from the CLI and make changes in Visual Studio, including changing _CMakeLists.txt_. Something that would work is to modify _CMakePresets.json_ in that .cmd, but I wonder what is the right way to do this.

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:** [December 4, 2023, 1:04pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/2 "2023-12-04T13:04:16Z")

</div>

Are you trying to find dependencies? If so, `find_package` is the way to wrap up the search process (which should ideally be seamless for default setups). Other than that, presets sound fine…but I don’t know presets that well.

The LLMs are hallucinating here; there is no `LOADS` generator expression (and is completely wrong anyways as you likely need your logic during configure and genexes are strictly post-configure information.

---

<div class="post-metadata">

**Author:** ![Ciobi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ciobi/32/3859_2.png) [@Ciobi](https://discourse.cmake.org/u/Ciobi)\
**Post date:** [December 4, 2023, 2:44pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/3 "2023-12-04T14:44:10Z")

</div>

Yes, I’m trying to find dependencies. Here are github links to the actual project: If the [environment variables](https://github.com/mciobanu/mp3diags/blob/138a17636e3e994b10d64770620399146a0a6bbc/CMakePresets.json#L20-L22) are not defined, the [find\_package() calls](https://github.com/mciobanu/mp3diags/blob/138a17636e3e994b10d64770620399146a0a6bbc/CMakeLists.txt#L67-L70) will fail.

(Well, technically I check if they are defined [above](https://github.com/mciobanu/mp3diags/blob/138a17636e3e994b10d64770620399146a0a6bbc/CMakeLists.txt#L52-L61), and exit if they are not defined, but the `find_package()` calls fail if I remove these checks.)

It’s just that I don’t want to commit the environment settings, which are specific to my machine. On Linux things just work, but in Windows the libraries are not found even though I installed them to their default locations.

So my question is if there is a way to have the environment variables in an external file, while keeping the `"windows-base"` preset where it is, in `CMakePresets.json`. Or, given that I know next to nothing about CMake, what is a better way to handle dependencies? Perhaps if I just installed Boost or Qt somewhere else, or if I searched for Boost or Qt in a different way, CMake would find them without needing any environment variable or CLI param, as it finds them in Linux?

---

<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:** [December 6, 2023, 1:02pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/4 "2023-12-06T13:02:25Z")

</div>

I’m not sure that presets are best for this, but I’m also not very familiar with preset patterns.

I would solve this by documenting to add any relevant prefixes to `CMAKE_PREFIX_PATH` which is a `;`-separated list (as a variable) or a path-list environment variable (`;`-separated on Windows and `:` elsewhere). You can then do:

```auto
cmake "-DCMAKE_PREFIX_PATH=c:/prefix/for/qt;c:/prefix/for/boost" /path/to/src

```

If you want, you can do this in your CMake code:

```cmake
list(APPEND CMAKE_PREFIX_PATH
  $ENV{MYPROJ_QT_PREFIX}
  $ENV{MYPROJ_BOOST_PREFIX}
)

```

before the `find_package` calls and document the specific envvars instead of the more “general” way.

---

<div class="post-metadata">

**Author:** ![Ciobi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/ciobi/32/3859_2.png) [@Ciobi](https://discourse.cmake.org/u/Ciobi)\
**Post date:** [December 7, 2023, 9:57am UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/5 "2023-12-07T09:57:45Z")

</div>

Thanks, Ben.

My understanding is that starting with _Visual Studio 2019_, you are supposed to just open _CMakeLists.txt_. There is no invoking `cmake` directly, as far as I can tell. The old way of generating a _Visual Studio_ `.sln` still works, but, well, it is old, and doesn’t seem to bring a real advantage.

So I guess I’ll leave it at telling the users to adjust `CMakePresets.json,` unless I learn of something better.

---

<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:** [December 11, 2023, 1:29pm UTC](https://discourse.cmake.org/t/specifying-system-specific-environment-variables/9546/6 "2023-12-11T13:29:37Z")

</div>

There are still some things that the Visual Studio generator supports that the Ninja generator doesn’t, but if you don’t need them, yes, just using the native-provided CMake integration is probably sufficient.
