Skip to content

Instantly share code, notes, and snippets.

@acceptable-security
Created December 7, 2014 18:05
Show Gist options
  • Save acceptable-security/f85082b4a716adb5f7ff to your computer and use it in GitHub Desktop.
Save acceptable-security/f85082b4a716adb5f7ff to your computer and use it in GitHub Desktop.
A general usage Makefile for c/c++
# Brian Smith's Super Duper Makefile
# Change CC/CFLAGS/LDFLAGS to fit your needs
# Directory format it searchs for:
# src/ - have your .c/.cc/.cpp files in there
# include/ - have your .h/.hpp files in there
# build/ - empty folder for object files and the executable
# CHANGE THESE TO FIT YOUR NEEDS
CC = cc
CFLAGS = -c -Wall -Iinclude/
LDFLAGS = -lpthread
# DONT TOUCH AFTER THIS - IT'S ALL AUTO
NAME = $(notdir $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))))
EXT = .c
SOURCES = $(strip $(wildcard src/*.c))
ifeq ($(SOURCES),)
EXT = .cpp
SOURCES = $(strip $(wildcard src/*.cpp))
endif
ifeq ($(SOURCES),)
EXT = .cc
SOURCES = $(strip $(wildcard src/*.cc))
endif
ifeq ($(SOURCES),)
$(error Unable to find the files!)
endif
OBJECTS = $(subst src/, build/, $(SOURCES:$(EXT)=.o))
EXECUTABLE = build/$(NAME)
TAR = $(NAME).tar
.PHONY: depend clean
all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@
$(OBJECTS):
$(CC) $(CFLAGS) $(subst .o,$(EXT), $(subst build/, src/, $@)) -o $@
clean:
rm -f build/*.o $(EXECUTABLE) $(TAR)
tar:
tar cfv $(TAR) $(SOURCES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment