I am working on a C++ project with a makefile. I have to make several modifications to it but before that, I have a question about the way flags are interpreted in GNU make. To elaborate, in the following snippet, I have two options to enable or disable a feature during the compilation of my project,
# Two options for a feature:
FEATURE=on off
# other lines in the make file
# By default, the feature is turned off
ifndef FEATURE
FEATURE = off
endif
# other lines in the make file
# A number of other flags are defined here
# I have defined a flag to indicate that my feature is disabled
CXXFLAGS_off = -DFEATURE_DISABLED
# Adding all the flags
CXXFLAGS += $(CXXFLAGS_$(FEATURE)) #Other flags also added
Now, somewhere in my code, I have this line:
#ifdef FEATURE_DISABLED
//Don't invoke the functions for the feature
#else
//Invoke the functions for the feature
#endif
Now during compilation, when I say make FEATURE = on , I see that the program works fine, with the feature enabled. When I say make FEATURE = off, it is disabled.
My problem however is that I don't exactly understand how the compiler is interpreting my choice. For example, I am just saying, "make FEATURE = off", how is this line mapped to the fact that the flag for off is enabled and how does the code get compiled with the feature turned off? As I wrote above, I do add the flags for my feature to the CXXFLAGS, but how does make understand that FEATURE = off means that the FEATURE_DISABLED flag is set?
Thank you very much for any explanation.
Because you wrote
CXXFLAGS += $(CXXFLAGS_$(FEATURE))
which, when you supply FEATURE = off
on the make
command line, will expand to
CXXFLAGS += $(CXXFLAGS_off)
which, because you have also defined
CXXFLAGS_off = -DFEATURE_DISABLED
in turn expands to
CXXFLAGS += -DFEATURE_DISABLED
which means that the compiler will be run with -DFEATURE_DISABLED
as an extra argument.