[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is my Makefile okay?
Re: Is my Makefile okay?
Sun, 08 Jan 2012 22:31:09 -0500
On Sun, 2012-01-08 at 22:18 -0500, Jeffrey Walton wrote:
> On Sun, Jan 8, 2012 at 9:36 PM, Pelias <address@hidden> wrote:
> > I'm new to GNU make so I would like to know if I made any mistakes in the
> > way
> > I have written my Makefile.
> > One thing I'm not sure whether to append -DDEBUG/-DNDEBUG to CPPFLAGS or to
> > CFLAGS.
> If your make file is using both C and C++ files (and using implicit
> rules), you will want to apply DEBUG/NDEBUG to both. Otherwise, use
> CFLAGS for C and CPPFLAGS for C++.
It's a common misconception, but CPPFLAGS is not for C++. It's an
unfortunate overlap that "CPP" is appropriate for both "C plus plus" and
"C pre-processor", but the latter was common before C++ was even
invented, and make dates back to those earlier days as well.
It breaks down like this:
CFLAGS: Compiler flags for the C compiler
CXXFLAGS: Compiler flags for the C++ compiler
CPPFLAGS: Preprocessor flags for both C and C++ preprocessor.
CPPFLAGS exists separately because the preprocessor flags (-I, -D, -U,
etc.) are useful for other tools which parse C/C++ code, such as lint,
etc. and want to have the same set of preprocessor flags as the
So, it is appropriate to add -DNDEBUG to CPPFLAGS.
Your makefile is fine for a small, simple project. If it starts to get
more complex you might consider a few enhancements:
%.o: %.cc $(HEADERS)
may not be what you want. With this line any change to any header file
will cause ALL object files to be recreated. Typically you want to
define only those headers that are used by a given file. There are
automated ways to compute this.
My personal preference is to NOT use $(wildcard ...) for source lists.
It's OK for small, simple projects but beyond that I prefer explicit
lists. It helps avoid misunderstandings and you don't add new files so
often that updating the makefile is a burden.
Finally when things get more complex you might consider modifying the
makefile so that objects are placed in a different directory than the
sources. In fact you can have one "debug" directory and one "optimized"
directory; this avoids the different extensions (.o vs. .do) since you
have have both use .o, just in different directories. Etc.
Also it makes cleanup simpler.
Paul D. Smith <address@hidden> Find some GNU make tips at:
"Please remain calm...I may be mad, but I am a professional." --Mad Scientist