captain_flammy
captain_flammy

Reputation: 137

How to do conditional variable assignment with cmake?

I would like to give a default value DEF to a variable VAR if I do not give the flag -DVAR=VAL. Have you any idea?

Thank you.

Upvotes: 0

Views: 6043

Answers (2)

Florian
Florian

Reputation: 42842

You can just use set(... CACHE ...) command:

set(VAR "DEF" CACHE STRING "My VAR for doing xyz")

If the variable is already set in the cache - e.g. via command line switch -D - it's not overwritten. And the user could change it with or .

Or if you have boolean values take the option() definitions command.

References

Upvotes: 1

captain_flammy
captain_flammy

Reputation: 137

The following seems to work. Thanks to Kamil Cuk which leads me to one solution.

if(not defined VAR)
                set(VAR VAL)
 endif()

Upvotes: 2

Related Questions