Cmake Set Environment Variable

cmake : Set environment variables from a script

Reading through the cmake quick start, you can specify variable on a command line:

cmake -DVARIABLE1=value1 -DVARIABLE2=value2 ...

Otherwise, set command in the cmake script is probably what you want, see the reference manual. To set the environment variable PATH, do:

set(ENV{PATH} "/home/martink")

To set normal variable, do:

set(variable "value")

Not sure which ones you have to set, probably the environment ones.

That said, setting environment variable prior to calling cmake is often the easiest solution to solve the problem. If you want a cross-platform way to do this that doesn't depend on the syntax of a specific shell to set environment variables, there is the cmake -E env command.

How to set environment variables in CMake so that they can be visible at build time?

Maybe what you want is CMAKE_XCODE_ATTRIBUTE_<an-attribute>

https://cmake.org/cmake/help/latest/variable/CMAKE_XCODE_ATTRIBUTE_an-attribute.html

With CLion, how do I set an environment variable for the cmake build?

According to FAQ

Q: How to pass environment variables and parameters to CMake in CLion?

A: The best way is to use Preferences/Settings | Build, Execution, Deployment | CMake dialog.

So: File > Settings > Build, Execution, Deployment > CMake

There's Environment field in the dialog.

CMake Environmental variables

Nothing bad in using environment variables in CMakeLists.txt.

For example, many FindXXX.cmake scripts actually use environment variables.

What pitfalls must be expected with such usage?

  1. After reading a path from the environment variable, transform it into cmake-style path (e.g. with file(TO_CMAKE_PATH)) before access a file at this path in CMake commands. Otherwise you could get a problem on Windows, which path separator ("\") differs from the CMake one ("/").

  2. Because setting environment variable affects only on configuration stage, this is rarely needed: better to use CMake variables instead.

    The only important exception: a toolchain file could be executed multiple times during the configuration. And setting an environment variable is the only way to store a value, calculated on the first toolchain invocation, for being usable in futher invocations. (Even CACHE variables don't work in that case).



Related Topics



Leave a reply



Submit