Created
July 31, 2009 21:44
-
-
Save mkhl/159461 to your computer and use it in GitHub Desktop.
Small and generic Makefile skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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