Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String Generator Expression gives unexpected result

I have the following CMake code:

add_library(iflib INTERFACE)
target_compile_definitions(iflib INTERFACE
    GEN_EXP=$<IF:$<STREQUAL:"test","test">,"EQUAL","DIFFERENT">
)

Which sets the GEN_EXP definition to "EQUAL", as expected.

However, when I compare with a variable:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

add_library(iflib INTERFACE)
set(TEST_VAR "test")
target_compile_definitions(iflib INTERFACE
    GEN_EXP=$<IF:$<STREQUAL:${TEST_VAR},"test">,"EQUAL","DIFFERENT">
)

The GEN_EXP definition becomes "DIFFERENT".

Is it not possible to do this kind of comparison in Generator Expressions?

>Solution :

With the variable evaluated the generator expression looks like this:

target_compile_definitions(iflib INTERFACE
    GEN_EXP=$<IF:$<STREQUAL:test,"test">,"EQUAL","DIFFERENT">
)

depending on whether you want to check for quotes in the variable value or not you either need to add the quotes to the content of the variable

set(TEST_VAR "\"test\"")

or change the generator expression removing the quotes around the value to compare to

target_compile_definitions(iflib INTERFACE
    GEN_EXP=$<IF:$<STREQUAL:${TEST_VAR},test>,"EQUAL","DIFFERENT">
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading