I have following Makefile, and I would like to configure it to produce debug build by default and release build by specifying corresponding target.
The problem I am
Debug and release targets get build with different macros (e.g. NDEBUG
) and different optimization levels. You normally do not want to mix debug and release object files, hence they need to be compiled into different directories.
I often use a different top-level build directory for different build modes this way:
BUILD := debug
BUILD_DIR := build/${BUILD}
And compiler flags this way:
cppflags.debug :=
cppflags.release := -DNDEBUG
cppflags := ${cppflags.${BUILD}} ${CPPFLAGS}
cxxflags.debug := -Og
cxxflags.release := -O3 -march=native
cxxflags := -g -pthread ${cxxflags.${BUILD}} ${CXXFLAGS}
And then build your objects into BUILD_DIR
.
When you invoke it as make BUILD=release
that overrides BUILD := debug
assignment made in the makefile.