bash 如何检查cmake中是否设置了环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29688115/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:56:06  来源:igfitidea点击:

How do I check if an environment variable is set in cmake

bashcmakeenvironment-variables

提问by learnvst

I set an environment variable in my bash profile, so I can see it in the terminal just fine . .

我在我的 bash 配置文件中设置了一个环境变量,所以我可以在终端中看到它就好了。.

blah/builds$ echo $THING

thingy

废话/构建$ echo $THING

东西

How do I display it in a cmake message and check if it is set? I have tried the following, but it just displays thing as blank and skips the body of the if statement

如何在 cmake 消息中显示它并检查它是否已设置?我尝试了以下操作,但它只是将内容显示为空白并跳过 if 语句的主体

message("THING:" $ENV{THING})
if(DEFINED ENV{THING})
    message(STATUS "THING environment variable defined")
    # some more commands
endif()

采纳答案by kynan

You CMake code is correct. The problem is most likely that you only setthe environment variable in your shell but did not exportit. Run the following before invoking cmake:

您的 CMake 代码是正确的。问题很可能是您只在 shell 中设置了环境变量,但没有导出它。在调用之前运行以下命令cmake

export THING

回答by ulidtko

Knowing perfectly well what exportdoes and how environment works in general, I've still got a mouthful of WTFs with this form:

非常清楚export环境的作用和一般情况下如何工作,我仍然有很多具有这种形式的 WTF:

IF(DEFINED $ENV{THING})

but it worked fine in this form:

但它以这种形式运行良好:

IF(DEFINED ENV{THING})

Notice the $omission.

注意$省略。



N.B. you can quickly test this using cmake -P:

注意,您可以使用cmake -P以下方法快速测试:

[~] cat > test-env.cmake << 'EOF'
IF(DEFINED ENV{FOOBAR})
    MESSAGE(STATUS "FOOBAR env seen: --[$ENV{FOOBAR}]--")
ELSE()
    MESSAGE(STATUS "WTF")
ENDIF()
EOF
[~]
[~] FOOBAR=test cmake -P test-env.cmake
-- FOOBAR env seen: --[test]--


The reason it behaves so weirdly, is legacy, as usual. IFused to do insane stuff in older CMake; they sort-of fixed this in CMake 3.1 — in a backward-compatible manner — with CMP0054, which you have to enable explicitly:

像往常一样,它表现得如此怪异的原因是遗留问题。IF曾经在旧的 CMake 中做疯狂的事情;他们在 CMake 3.1 中以向后兼容的方式修复了这个问题,使用CMP0054,您必须明确启用它:

CMAKE_MINIMUM_REQUIRED(VERSION 3.1)
PROJECT(...)

CMAKE_POLICY(SET CMP0054 NEW) #-- fixes IF() with quoted args
CMAKE_POLICY(SET CMP0057 NEW) #-- enables IF(.. IN_LIST ..)

回答by ToyAuthor X

I did this but it doesn't work. CMake can't detect it.

我这样做了,但它不起作用。CMake 无法检测到它。

export THING

But this one is works.

但这是一个作品。

export THING=on

Maybe I should always give default value for environment variable.

也许我应该始终为环境变量提供默认值。

By the way, you can check the environment string by following CMake code.

顺便说一下,您可以按照 CMake 代码检查环境字符串。

if( $ENV{THING} STREQUAL "on")
    message(STATUS "THING = " $ENV{THING})
endif()

回答by Jan Wilmans

Just to be clear: as https://cmake.org/cmake/help/latest/command/if.htmlsays:

只是要清楚:正如https://cmake.org/cmake/help/latest/command/if.html所说:

if (DEFINED ENV{THING})
  # do stuff
endif()

is the right syntax, no $ anywhere.

是正确的语法,任何地方都没有 $。

回答by Gaurav

Replace

代替

if(DEFINED ENV{THING})

with

if(DEFINED $ENV{THING})

You missed a '$' before the variable.

您在变量之前错过了一个 '$'。