Created
September 21, 2016 23:02
-
-
Save djnugent/289442309f32cdae6bbf5d860189fa13 to your computer and use it in GitHub Desktop.
Universal C/C++ Makefile
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
#https://ubuntuforums.org/showthread.php?t=1204739 | |
app = shell | |
srcExt = c | |
srcDir = src | |
objDir = build | |
binDir = . | |
inc = include | |
debug = 1 | |
CFlags = -Wall | |
LDFlags = | |
libs = | |
libDir = | |
#************************ DO NOT EDIT BELOW THIS LINE! ************************ | |
ifeq ($(debug),1) | |
debug=-g | |
else | |
debug= | |
endif | |
inc := $(addprefix -I,$(inc)) | |
libs := $(addprefix -l,$(libs)) | |
libDir := $(addprefix -L,$(libDir)) | |
CFlags += -c $(debug) $(inc) $(libDir) $(libs) | |
sources := $(shell find $(srcDir) -name '*.$(srcExt)') | |
srcDirs := $(shell find . -name '*.$(srcExt)' -exec dirname {} \; | uniq) | |
objects := $(patsubst %.$(srcExt),$(objDir)/%.o,$(sources)) | |
ifeq ($(srcExt),cpp) | |
CC = $(CXX) | |
else | |
CFlags += -std=gnu99 | |
endif | |
.phony: all clean distclean | |
all: $(binDir)/$(app) | |
$(binDir)/$(app): buildrepo $(objects) | |
@mkdir -p `dirname $@` | |
@echo "Linking $@..." | |
@$(CC) $(objects) $(LDFlags) -o $@ | |
$(objDir)/%.o: %.$(srcExt) | |
@echo "Generating dependencies for $<..." | |
@$(call make-depend,$<,$@,$(subst .o,.d,$@)) | |
@echo "Compiling $<..." | |
@$(CC) $(CFlags) $< -o $@ | |
clean: | |
$(RM) -r $(objDir) | |
distclean: clean | |
$(RM) -r $(binDir)/$(app) | |
buildrepo: | |
@$(call make-repo) | |
define make-repo | |
for dir in $(srcDirs); \ | |
do \ | |
mkdir -p $(objDir)/$$dir; \ | |
done | |
endef | |
# usage: $(call make-depend,source-file,object-file,depend-file) | |
define make-depend | |
$(CC) -MM \ | |
-MF $3 \ | |
-MP \ | |
-MT $2 \ | |
$(CFlags) \ | |
$1 | |
endef |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment