Issue
I have a preprocessing constant in my code :
const unsigned int nbins= _NBINS;
And I would like to be able to change it when compiling. For example, I would like to be able to write:
cmake build_path [string to change _NBINS]
How can I do it using cmake?
Edit: I usually use makefiles so I was a bit confused For those who ask why I want to do that, it is because I want to autotest my program with various values for _NBINS. I want to recompile my program each time with the new value for _NBINS automatically. _NBINS needs to be a constant because its value is critical for optimisation purposes
Solution
How can I do it using cmake?
You can pass the value of your constant into a cmake variable then inside a cmake script get the value of that constant and pass it to compiler.
Call cmake with:
cmake build_path -D NBINS=something
And inside CMakeLists.txt
:
add_executable(your_target ...)
target_compile_definitions(your_target PUBLIC NBINS=${NBINS})
or before the target:
add_compile_definitions(NBINS=${NBINS})
Note that in C++ identifiers with leading _
followed by uppercase letters are reserved. Do not use them in your code. Because of that, note I have removed the leading _
in the code snippets above.
Answered By - KamilCuk