The basic thing one has to do is making every object depend on a config file.
As a slightly insane solution for c/c++ one can use a file like below which is correct syntax for both makefiles and c/c++.
Instead of having all the compiler flags in the Makefile itself, I create the following file "Makefile_flags":
#undef DUMMY
#define DUMMY /*
PROFILING_FLAGS = -p -g -pg
OPTIMIZATION_FLAGS = -O3
COMPILE_FLAGS = -Wall -Wextra -Wuninitialized -Wmissing-declarations \
-Wshadow -ftrapv -Wfloat-equal -Wundef -Wpointer-arith \
-Wcast-align -Wunreachable-code -Wold-style-cast \
-Wformat=2 -Winit-self -Werror-implicit-function-declaration \
-Wredundant-decls -Wunsafe-loop-optimizations \
-pedantic -MD -MP
CPP_STD_FLAGS = -std=c++0x
COMPILE_FLAGS += $(CPP_STD_FLAGS)
COMPILE_FLAGS += $(PROFILING_FLAGS)
COMPILE_FLAGS += $(OPTIMIZATION_FLAGS)
LINKING_FLAGS = $(COMPILE_FLAGS)
#foo */
Now write -include Makefile_flags
in your Makefile and #include "Makefile_flags"
in every file of your source code you want to have updated (e.g. in every *.c / *.cpp file).
The beauty of this solution: The Makefile uses #
as symbol for comments, thus #undef DUMMY
, #define DUMMY /*
and #foo */
have no effect here. In C/C++ however, /*
is used for multiline comments. Thus, the whole non-C-code is ignored by the compiler and the unknown symbol /*
is not seen by the Makefile. Additionally, the pre-processor instruction #undef DUMMY
takes care of not doing #define DUMMY
twice and the #foo
statement is inside of the multi-line-comment.
The downside however is, that one must include it in every file.
Make sure that you have the right relative path to your file "Makefile_flags".