CMake扩展环境变量具有自定义文件的默认值 [英] CMake expand env variable with default value for custom file
问题描述
使用CMake,我正在尝试在自定义文件中扩展系统环境变量值。
我执行以下命令:
Using CMake I'm trying to expand system environment variable values in custom file. I do the following command:
configure_file(config.cnf.in config.cnf)
config.cnf.in 内容:
[options]
some_value1 = $ENV{SYSTEM_ENV_VAR}
问题是:
是否可以为 SYSTEM_ENV_VAR 变量设置默认值?定义的?
The question is: Is it possible to set the default value for SYSTEM_ENV_VAR variable, if it is not defined?
我尝试这样做:
some_value1 = $ENV{SYSTEM_ENV_VAR:-defaultValue}
并收到cmake错误:
无效字符(':' )的变量名:'SYSTEM_ENV_VAR'
and got an cmake error: Invalid character (':') in a variable name: 'SYSTEM_ENV_VAR'
我在文档中找不到答案:
https://cmake.org/cmake/help/v3.2/command/configure_file.html
I didn't find the answer in the docs: https://cmake.org/cmake/help/v3.2/command/configure_file.html
推荐答案
您不能为变量设置默认值,但是可以将环境变量存储在普通的cmake变量中,如果未设置定义默认值。
我对此进行了测试:
You cannot set a default value for the variable, but you can store the environment variable inside a normal cmake variable, and if it was not set define a default value. I tested this:
set(EXIST $ENV{HOME})
set(NOT_EXIST $ENV{NOT_EXIST})
if(EXIST)
message("Variable EXIST exist")
else()
message("Variable EXIST DOES NOT exist, setting default value")
set(EXIST "Default value")
endif()
if(NOT_EXIST)
message("Variable NOT_EXIST exist")
else()
message("Variable NOT_EXIST DOES NOT exist, setting default value")
set(NOT_EXIST "Default value")
endif()
message ("EXIST: ${EXIST}")
message ("NOT_EXIST: ${NOT_EXIST}")
我的输出是
Variable EXIST exist
Variable NOT_EXIST DOES NOT exist, setting default value
EXIST: /home/user
NOT_EXIST: Default value
这篇关于CMake扩展环境变量具有自定义文件的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!