Skip to content

Instantly share code, notes, and snippets.

@mkhl
Created July 31, 2009 21:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhl/159461 to your computer and use it in GitHub Desktop.
Save mkhl/159461 to your computer and use it in GitHub Desktop.
Small and generic Makefile skeleton
PROJECT=foo
SOURCES=bar.c baz.c
LIBRARY=nope
INCPATHS=../some_other_project/
LIBPATHS=../yet_another_project/
LDFLAGS=-ldosomething
CFLAGS=-c -Wall
CC=gcc
# ------------ MAGIC BEGINS HERE -------------
# Automatic generation of some important lists
OBJECTS=$(SOURCES:.c=.o)
INCFLAGS=$(foreach TMP,$(INCPATHS),-I$(TMP))
LIBFLAGS=$(foreach TMP,$(LIBPATHS),-L$(TMP))
# Set up the output file names for the different output types
ifeq "$(LIBRARY)" "shared"
BINARY=lib$(PROJECT).so
LDFLAGS += -shared
else ifeq "$(LIBRARY)" "static"
BINARY=lib$(PROJECT).a
else
BINARY=$(PROJECT)
endif
all: $(SOURCES) $(BINARY)
$(BINARY): $(OBJECTS)
# Link the object files, or archive into a static library
ifeq "$(LIBRARY)" "static"
ar rcs $(BINARY) $(OBJECTS)
else
$(CC) $(LIBFLAGS) $(OBJECTS) $(LDFLAGS) -o $@
endif
.c.o:
$(CC) $(INCFLAGS) $(CFLAGS) -fPIC $< -o $@
distclean: clean
rm -f $(BINARY)
clean:
rm -f $(OBJECTS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment