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?
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 ("/").
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
How to Write to Tty from a Kernel Module
What Does Grep -Po '...\K...' Do? How Else Can That Effect Be Achieved
How to Set Runpath of a Binary
How to Monitor Newly Created File in a Directory with Bash
Case in Bash: "Line 4: Syntax Error Near Unexpected Token ')'"
How to Disable Specific Warning When -Wall Is Enabled
Mod_Rewrite with Relative Path Redirects
How to Configure a Specific Ip in Prometheus Yml Configuration File
How to Initialize the Attribute Group Correctly for a Platform Driver
Zipping Without Creating Parent Folder
Difference Between Trap Flag (Tf) and Monitor Trap Flag
How to Uniquely Identify an Usb-Device