如何在 CMake (Windows) 中检索用户环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/690223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve a user environment variable in CMake (Windows)
提问by Lars Bilke
I know how to retrieve a normal machine wide environment variable in CMAKE using
我知道如何在 CMAKE 中使用检索正常的机器范围的环境变量
$ENV{EnvironmentVariableName}
but I can not retrieve a user specific environment variable. Is it possible and how?
但我无法检索用户特定的环境变量。有可能吗?
采纳答案by Cameron Lowell Palmer
Getting variables into your CMake script
将变量添加到 CMake 脚本中
You can pass a variable on the line with the cmake invocation:
您可以使用 cmake 调用在行上传递一个变量:
FOO=1 cmake
or by exporting a variable in BASH:
或者通过在 BASH 中导出变量:
export FOO=1
Then you can pick it up in a cmake script using:
然后您可以使用以下命令在 cmake 脚本中获取它:
$ENV{FOO}
回答by Florian
You can also invoke cmakeitself to do this in a cross-platform way:
您还可以调用cmake本身以跨平台的方式执行此操作:
cmake -E env EnvironmentVariableName="Hello World" cmake ..
env [--unset=NAME]... [NAME=VALUE]... COMMAND [ARG]...
Run command in a modified environment.
env [--unset=NAME]... [NAME=VALUE]... COMMAND [ARG]...
在修改后的环境中运行命令。
Just be awarethat this may only work the first time. If CMake re-configures with one of the consecutive builds (you just call e.g. make
, one CMakeLists.txt
was changed and CMake runs through the generation process again), the user defined environment variable may not be there anymore (in comparison to system wide environment variables).
请注意,这可能仅在第一次有效。如果 CMake 使用连续构建之一重新配置(您只需调用例如make
,一个CMakeLists.txt
被更改并且 CMake 再次运行生成过程),则用户定义的环境变量可能不再存在(与系统范围的环境变量相比)。
So I transfer those user defined environment variables in my projects into a CMake cached variable:
所以我将项目中那些用户定义的环境变量转移到 CMake 缓存变量中:
cmake_minimum_required(VERSION 2.6)
project(PrintEnv NONE)
if (NOT "$ENV{EnvironmentVariableName}" STREQUAL "")
set(EnvironmentVariableName "$ENV{EnvironmentVariableName}" CACHE INTERNAL "Copied from environment variable")
endif()
message("EnvironmentVariableName = ${EnvironmentVariableName}")
Reference
参考
回答by P51DAce
You need to have your variables exported. So for example in Linux:
您需要导出变量。例如在 Linux 中:
export EnvironmentVariableName=foo
Unexported variables are empty in CMAKE.
未导出的变量在 CMAKE 中为空。
回答by Mark Lakata
Environment variables (that you modify using the System Properties) are only propagated to subshells when you create a new subshell.
环境变量(您使用系统属性修改的)仅在您创建新子 shell 时传播到子 shell。
If you had a command line prompt (DOS or cygwin) open when you changed the User env vars, then they won't show up.
如果您在更改用户环境变量时打开了命令行提示符(DOS 或 cygwin),则它们不会显示。
You need to open a new command line prompt after you change the user settings.
更改用户设置后,您需要打开一个新的命令行提示符。
The equivalent in Unix/Linux is adding a line to your .bash_rc: you need to start a new shell to get the values.
Unix/Linux 中的等价物是在 .bash_rc 中添加一行:您需要启动一个新的 shell 来获取值。