# Feature Request: Ability to set environment variables at build time

**URL:** https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159
**Category:** Development
**Created:** [November 12, 2020, 2:18am UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159 "2020-11-12T02:18:10Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Matt\_Rogers](https://discourse.cmake.org/user_avatar/discourse.cmake.org/matt_rogers/32/582_2.png) [@Matt\_Rogers](https://discourse.cmake.org/u/Matt_Rogers)
#### Post date: [November 12, 2020, 2:18am UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/1 "2020-11-12T02:18:10Z")

</div>

At my day job, I maintain a few custom toolchain files for cross-compiling. One of the problems I ran into when trying to add another one, is that the compiler requires several variables to be set. I’ve struggled to find any resources about setting environment variables at build time (only during the configuration step).

I can’t simply just add a custom target to modify the environment at build time, since the Ninja generator (which is one of the ones that I support) runs all of its commands in subshells.

The two workarounds I’ve come up with are:

1. Create a script to configure the environment variables that a user can run pre-build. (I don’t like this since it would be easy for a user to forget to run it, and our build procedures can get pretty complicated for those unfamiliar with it)

2. Create a wrapper script/program to use as the compiler. This would look something like:

```auto
REM compiler-wrapper.bat
SET VAR1=value
SET VAR2=other_value

%PATH_TO_COMPILER% %*

```

This doesn’t quite feel right as a solution.

I understand that not all build systems support setting environment variables out of the box, but it would definitely simplify this situation if CMake was able to handle this for me.

> **Summary**
>
> This text will be hidden

---

<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: [November 12, 2020, 12:20pm UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/2 "2020-11-12T12:20:07Z")

</div>

CMake generally assumes that the compiler is usable from the environment in which it is run. This is the case with MSVC, Intel, etc. I don’t think a mechanism to move environment settings to build time will be implemented because there’s no way to do something like that for `make`, `ninja`, or other generators anyways without requiring that one only use `cmake --build .` to do the build.

However, there are other potential solutions. If you do have such an environment script, you may be able to use it via `CMAKE_<LANG>_COMPILER_LAUNCHER`. This gets placed as the first argument for any compilation line (though maybe not linker?) for the compiler in the build. You’ll get the rest of the compilation line as arguments to the script (so that you don’t have to embed anything about compiler paths either). Note that `rc.exe` will still be unwrapped, so that won’t help you there if you need it.

In summary, the best solution available today is to just source `vcvarsall.bat` (or whatever you need) before you run cmake or the build.

---

<div class="post-metadata">

### Author: ![Matt\_Rogers](https://discourse.cmake.org/user_avatar/discourse.cmake.org/matt_rogers/32/582_2.png) [@Matt\_Rogers](https://discourse.cmake.org/u/Matt_Rogers)
#### Post date: [November 12, 2020, 11:35pm UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/3 "2020-11-12T23:35:30Z")

</div>

Thanks for the quick reply, I’m gonna probably go forward with using the `CMAKE_<LANG>_COMPILER_LAUNCHER` since that seems to be the most idiomatic way to do what I want.

I do want to point out however, that some build systems do support setting environment variables:

For [make](https://stackoverflow.com/a/49524393/7869583) you can apparently using the `export` keyword, and in .sln/.vcproj files like they do in this [forum posting](https://social.msdn.microsoft.com/Forums/en-US/0fb8d97d-513e-4d37-8528-52973f65a034/how-to-set-envrionment-variables-in-msbuild-file?forum=msbuild) (which is a workaround using a custom task, so maybe not the best). ninja would be the only one that doesn’t support it at all (even though they do provide the `ninja -t msvc -e` command which sets the environment according to a file.

I think that this would definitely be a nice to have, if for nothing but setting up stuff like LD\_LIBRARY\_PATH, etc.

---

<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: [November 13, 2020, 3:11am UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/4 "2020-11-13T03:11:50Z")

</div>

> [@Matt\_Rogers](#):
>
> I think that this would definitely be a nice to have, if for nothing but setting up stuff like LD\_LIBRARY\_PATH, etc.

Use rpath settings and you won’t need LD\_LIBRARY\_PATH at all.

> [@Matt\_Rogers](#):
>
> For [make](https://stackoverflow.com/a/49524393/7869583) you can apparently using the `export` keyword

This looks like a GNU-ism. CMake generates POSIX-compatible makefiles.

You may be interested in an issue (that I can’t find right now) where I propose adding runtime usage requirements which would encompass things like `@rpath` usage on macOS and assist with `PATH` building on Windows.

---

<div class="post-metadata">

### Author: ![sdettmer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@sdettmer](https://discourse.cmake.org/u/sdettmer)
#### Post date: [December 4, 2020, 4:18pm UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/5 "2020-12-04T16:18:22Z")

</div>

Hi,

did you find the mentioned ticket? I also from time to time wished to be able to set variables; for example we have proprietary post build tools that need certain variables conditionally set. For these, we usually run them via …\_command(“VAR=value /path/to/tool”). For some, we wrote wrapper scripts supporting command line options from what was used to be an environment setting (which I like because usage improves, but add complexity of course).

For compilers and ccache, we use a LAUNCHER, which is generated by cmake, for example to unset GCC\_COLORS when using old compilers plus ccache (there are a lot of complex details).

---

<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, 2020, 8:57pm UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/6 "2020-12-04T20:57:30Z")

</div>

Sorry, no, I can’t find it in my email. Maybe it was an in-person discussion? I’ll have to write it up some day anyways…I want to reference it at least every other week these days…

---

<div class="post-metadata">

### Author: ![david-fong](https://discourse.cmake.org/user_avatar/discourse.cmake.org/david-fong/32/2388_2.png) [@david-fong](https://discourse.cmake.org/u/david-fong)
#### Post date: [October 21, 2025, 10:58am UTC](https://discourse.cmake.org/t/feature-request-ability-to-set-environment-variables-at-build-time/2159/7 "2025-10-21T10:58:13Z")

</div>

For what it’s worth, you can set environment variables in CMake Presets, and for steps of External Projects.
