Passing build variables using command line

I’d like to override the cmake cached variable using the command line. But if a variable is repeatedly provided, does the last value take effect?

# what is the value of FOO?
> cmake -DFOO=123 -DFOO=321

Test result shows that the last value takes effect. However, I couldn’t find any documentation describing such behavior. Is this behavior consistent across different OS? Should I write script based on this assumption?

This is easy to test:

$ cmake -Dfoo=first -Dfoo=second -P foo.cmake

Yes, the last takes place from my test as well. However, this behavior is not documented anywhere. I’m wondering if I can write script based on this assumption.

Given CMake’s backwards compatibility guarantees, it is likely something we’d need to guarantee in the future. Please open an issue for us to make sure it is in the documentation.

1 Like

Thanks! The issue has been created.

Documentation: override the same build variable multiple times from command line (#21707) · Issues · CMake / CMake · GitLab (