Suppose I have a C++ source file which looks like:
int main() {
...
#ifdef flag
// do something
#endif
...
#ifndef flag
// do something different
#endif
...
}
I know that for gcc I can use -D'flag' as a directive to the pre-processor to define flag and thus determine which part of the code above actually gets compiled, and that this can be used in a Makefile.
But how do I write a Makefile such that the source file gets re-built based on whether I want flag defined or not (with no other changes to any file, including the Makefile itself)?
I know that I can, for example (and the solution doesn't need to follow this if there is a better way!), define a variable when I run make e.g. with make flag=1 all, but this will not trigger a rebuild if the file containing main (in line with the source code example above) has not been changed since.
i.e. starting from a clean directory and running make all will run the build, but then if immediately I run make flag=1 all I would like the file containing main (say main.cpp) to be rebuilt correctly without having to touch main.cpp first.