[CMake] if (DEFINED $ENV{VAR}) doesn't work as expected

Glenn Coombs glenn.coombs at gmail.com
Tue Oct 11 17:46:22 EDT 2011


Yes, that did help.  Works perfectly without the $ character.

--
Glenn

On 11 October 2011 19:55, Michael Wild <themiwi at gmail.com> wrote:

> On 10/11/2011 06:02 PM, Glenn Coombs wrote:
> > Hi,
> >
> > I've just had a CMakeLists.txt fail to work as expected because somebody
> > was testing to see whether an environment variable was set with the
> > syntax: if (DEFINED $ENV{VAR}).  This short example shows the problem:
> >
> > cmake_minimum_required(VERSION 2.8)
> >
> > project(foo)
> >
> > message("HOME: $ENV{HOME}")
> > if (DEFINED $ENV{HOME})
> >     message("HOME is defined")
> > else()
> >     message("HOME is NOT defined")
> > endif()
> >
> > if (DEFINED FOO)
> >     message("At 1: FOO is defined")
> > else()
> >     message("At 1: FOO is NOT defined")
> > endif()
> >
> > set(FOO "foo")
> >
> > if (DEFINED FOO)
> >     message("At 2:FOO is defined")
> > else()
> >     message("At 2:FOO is NOT defined")
> > endif()
> >
> > When run it prints this:
> >
> > HOME: /user/grc
> > HOME is NOT defined
> > At 1: FOO is NOT defined
> > At 2:FOO is defined
> >
> > So the test for if a variable is defined works for cmake variables but
> > not for environment variables.  I can work around this by testing if the
> > environment variable is the empty string I guess.  Is the current
> > behaviour what is wanted, or is this a bug ?
> >
> > --
> > Glenn
>
> That's because it should read
>
> if(DEFINED ENV{VAR})
>
> notice the missing $. Otherwise you are testing whether a variable is
> defined whose name is given by the content of the VAR environment variable.
>
> HTH
>
> Michael
>
> --
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20111011/39f9876f/attachment.htm>


More information about the CMake mailing list