How do I change a cache variable which is used in my code

I have a boolean cache variable MY_VARIABLE that is used via configure_file and #ifdef to change what parts of my code are build. By default it is turned off. But if I change the variable with -DMY_VARIABLE=ON during the configure step the configure file is not updated.

That is probably because the input configure file has not changed. But how do I get MY_VARIABLE into my program so that #ifdef works? I tried to run cmake -E touch on my input configure file, but that also didn’t lead to any changes. The only way it works for me is to completely delete the build directory and reconfigure.

Thanks in advance for any pointers on this, I have been trying to get this to work for weeks.

I think I finally solved it, just as I made this post. In the end I started using #cmakedefine MY_VARIABLE and I simply misread the documentation. So the important (and surprising) part with #cmakedefine for me was that I did not need @'s around my variable!

So this is wrong: #cmakedefine @MY_VARIABLE@. Instead use #cmakedefine MY_VARIABLE.