Skip to content

Instantly share code, notes, and snippets.

@kladd
Created May 4, 2012 19:28
Show Gist options
  • Save kladd/2597176 to your computer and use it in GitHub Desktop.
Save kladd/2597176 to your computer and use it in GitHub Desktop.
C makefile
dep=$(shell ls *.h) # header files
all-sources=$(shell ls *.c) # source files
obj=$(all-sources:.c=.o) # objects
EXE=sample.exe # executable
CFLAGS_$@= # local C flags
# Check for verbosity flag
ifdef V
ifeq ("$(origin V)", "command line")
VERBOSE=$(V)
endif
else
VERBOSE=0
endif
# Set command prefixes based on verbosity
ifeq ($(VERBOSE), 1)
SAY=@true
Q=
else
SAY=echo
Q=@
endif
# if make -s (silent mode) suppress all output
ifneq ($(findstring s, $(MAKEFLAGS)),)
Q=@
SAY=@true
endif
# C compiling rules
all: $(EXE)
%.o: %.c $(dep)
$(Q)$(CC) -c -o $@ $< $(CFLAGS) $(CFLAGS_$@)
$(Q)$(SAY) "CC $@"
$(EXE): $(obj)
$(Q)$(CC) -o $@ $^ $(CFLAGS) $(CFLAGS_$@)
$(Q)$(SAY) "CC $@"
.PHONY: clean
clean:
$(Q)$(RM) *.o $(EXE)
$(Q)$(SAY) "Build directory clean"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment