patterncModerate
C Makefile boilerplate
Viewed 0 times
boilerplatemakefilestackoverflow
Problem
This is the current Makefile that I use with my C projects. Here is what I would like to be reviewed:
-
Reusability - is this Makefile easy to use for multiple separate projects with minimal modification?
-
Organization - is this Makefile organized in a logical and readable way?
-
Dynamics - are there Make features (or compiler options/warnings) that I am not taking advantage of that could make my Makefile more powerful?
-
Miscellaneous - what other things could I improve?
`CXX = g++-4.9
CC = gcc-4.9
DOXYGEN = doxygen
CFLAGS = -fdiagnostics-color=always -std=gnu11 -s -c -g3 -O3 -time
WARNINGS = -Werror -Wall -Wextra -pedantic-errors -Wformat=2 -Wno-import -Wimplicit -Wmain -Wchar-subscripts -Wsequence-point -Wmissing-braces -Wparentheses -Winit-self -Wswitch-enum -Wstrict-aliasing=2 -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wdisabled-optimization -Wunused-macros -Wno-unused
LDFLAGS =
LIBRARIES = -lcurl
SOURCES = main.c test.c
OBJECTS = $(SOURCES:.c=.o)
EXECUTABLE = test
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
debug: CFLAGS += -DDEBUG -g
debug: $(SOURCES) $(EXECUTABLE)
.c.o:
$(CC) $
-
Reusability - is this Makefile easy to use for multiple separate projects with minimal modification?
-
Organization - is this Makefile organized in a logical and readable way?
-
Dynamics - are there Make features (or compiler options/warnings) that I am not taking advantage of that could make my Makefile more powerful?
-
Miscellaneous - what other things could I improve?
`CXX = g++-4.9
CC = gcc-4.9
DOXYGEN = doxygen
CFLAGS = -fdiagnostics-color=always -std=gnu11 -s -c -g3 -O3 -time
WARNINGS = -Werror -Wall -Wextra -pedantic-errors -Wformat=2 -Wno-import -Wimplicit -Wmain -Wchar-subscripts -Wsequence-point -Wmissing-braces -Wparentheses -Winit-self -Wswitch-enum -Wstrict-aliasing=2 -Wundef -Wshadow -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wdisabled-optimization -Wunused-macros -Wno-unused
LDFLAGS =
LIBRARIES = -lcurl
SOURCES = main.c test.c
OBJECTS = $(SOURCES:.c=.o)
EXECUTABLE = test
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ $(LIBRARIES)
debug: CFLAGS += -DDEBUG -g
debug: $(SOURCES) $(EXECUTABLE)
.c.o:
$(CC) $
Solution
Dependencies: right now your makefile will let changes in
PS: it is highly recommended to remove
.h to remain unnoticed.DEPS := $(SOURCES:.c=.d)
.c.d:
$(CC) -o %%CODEBLOCK_0%%lt; -MM $(CFLAGS)
-include $(DEPS)PS: it is highly recommended to remove
-c from CFLAGS and mention it explicitly in the .c.o rule.Code Snippets
DEPS := $(SOURCES:.c=.d)
.c.d:
$(CC) -o $< -MM $(CFLAGS)
-include $(DEPS)Context
StackExchange Code Review Q#47188, answer score: 18
Revisions (0)
No revisions yet.