Skip to content

Instantly share code, notes, and snippets.

@zhester
Created May 14, 2013 15:44
Show Gist options
  • Save zhester/5576980 to your computer and use it in GitHub Desktop.
Save zhester/5576980 to your computer and use it in GitHub Desktop.
Simple drop-in gmake Makefile for stand-alone C projects that build with gcc.
##############################################################################
# Makefile
#
# Note: This assumes you have at least one header (.h) file in your project.
# If that is not the case, remove the "*.h" dependency from the rule to build
# object files. Or, just start using headers like a normal person.
##############################################################################
# Basic compile environment settings
CC = /usr/bin/gcc
CFLAGS = -Wall
LD = $(CC)
LDFLAGS = $(CFLAGS)
AR = /usr/bin/ar
ARFLAGS = -cr
SHELL = /usr/bin/sh
# Add debug symbols for any debug target.
debug: CFLAGS += -ggdb
debug: LDFLAGS += -ggdb
# Detect build environment.
UNAME := $(shell uname)
# Base name of the project and final binary
PROJ := $(shell basename `pwd`)
# Project source files
SOURCES := $(wildcard *.c)
# Build directory
BLDDIR = build
# Derive object targets from source files
OBJECTS := $(patsubst %.c, $(BLDDIR)/%.o, $(SOURCES))
# Default target
all: $(BLDDIR)/$(PROJ)
# How to build the project binary
$(BLDDIR)/$(PROJ): $(OBJECTS)
$(LD) $(LDFLAGS) -o $@ $(OBJECTS)
# How to build object files
$(BLDDIR)/%.o: %.c *.h | $(BLDDIR)
$(CC) $(CFLAGS) -o $@ -c $<
# Make sure there's an output directory.
$(BLDDIR):
mkdir -p $(BLDDIR)
# How to clean the output files
clean:
rm -rf $(BLDDIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment