[CMake] Question about IF and STRINGS

Kyle Edwards kyle.edwards at kitware.com
Fri May 31 13:31:59 EDT 2019


On Fri, 2019-05-31 at 19:07 +0200, Steven Truppe wrote:
> The problem is the line:
> if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")
> cmake_print_variables(CMAKE_MATCH_0)
> 
> doesn't print me any output ...

There are two problems with the following line:

if(${_var} MATCHES "^WITH_LIB_([AZaz]+)$")

The first argument should be _var instead of ${_var}. See the if()
documentation for more information:

https://cmake.org/cmake/help/v3.14/command/if.html#variable-expansion

The second problem is your regex. I'm guessing you were trying to do:

^WITH_LIB_([A-Za-z]+)$

Notice the hyphens in the character class brackets. This will get you
every character from A to Z and from a to z. Using just [AZaz] will
only match A, Z, a, and z (none of the letters in between.)

So, if you use the following instead:

if(_var MATCHES "^WITH_LIB_([A-Za-z]+)$")
  cmake_print_variables(CMAKE_MATCH_0)

this should do what you want.

Kyle


More information about the CMake mailing list