################################################################################ # Really Awesome Generic Makefile (for C and C++) ################################################################################ # # Created by: William Killian # Last Modified: 2012 August 27 22:10 # Contact: william.killian@gmail.com # # Instructions: # # Just change the following few lines. Everything else should be automatic # This is intended for use with C and C++ code. It can be extended with support # for any language/source files as long as there are explicitly defined rules # either in this makefile or a built-in rule as part of make (make -p). # ################################################################################ # TARGET PROPERTIES ################################################################################ # name of created executable TARGET_NAME := program # architecture for executable (-m32 or -m64) TARGET_ARCH := # FILE EXTENSIONS ################################################################################ # There are built-in recipes/rules. The following C/C++ extensions are supported SOURCE_EXT := .c,.cc,.cpp,.C # Uncomment the next line to add support for bison/yacc and flex/lex #SOURCE_EXT += ,.l,.y,.yy # These are useful for dependencies and tags (but not much else) HEADER_EXT := .h,.hh,.hpp,.tcc,.inl # COMPILERS ################################################################################ # c compiler CC := gcc # c++ compiler CXX := g++ # LINKER ################################################################################ # final linker (should usually be c++ compiler unless using pure c code) LINK = $(CXX) # FLAGS ################################################################################ # compiler flags for c compiler CFLAGS := -g -Wall -O2 -march=native # compiler flags for c++ compiler CXXFLAGS := -g -Wall -O2 -std=c++11 -march=native # flags to send to flex/lex LFLAGS := # flags to send to bison/yacc YFLAGS := # PREPROCESSOR FLAGS ################################################################################ # preprocessor flags to send to compiler (-D) CPPFLAGS := # INCLUDE ################################################################################ # paths to include in header file search (-I) INCPATHS := # LIBRARY ################################################################################ # paths to include in library search (-L) LDFLAGS := # libraries to include (-l) LDLIBS := ################################################################################ ################################################################################ # # DO NOT EDIT ANYTHING BEYOND THIS LINE UNLESS YOU KNOW WHAT YOU ARE DOING !!! # ################################################################################ ################################################################################ # Add Include Paths to c(xx)flags CFLAGS := $(CFLAGS) $(INCPATHS) CXXFLAGS := $(CXXFLAGS) $(INCPATHS) # Dependency options DEPS := Makefile.dep DEP_FLAG := -MM # Source Finder Regex ESCAPE_DOT := sed 's:\.:\\\.:g' # . ----> \. SEP_COMMA := sed 's:,:\\\|:g' # , ----> \| SOURCE_REGEX := ".*\($(shell echo $(SOURCE_EXT) | $(ESCAPE_DOT) | $(SEP_COMMA))\)" HEADER_REGEX := ".*\($(shell echo $(HEADER_EXT) | $(ESCAPE_DOT) | $(SEP_COMMA))\)" OBJECT_REGEX := "s:\($(shell echo $(SOURCE_EXT) | $(ESCAPE_DOT) | $(SEP_COMMA))\):.o:g" # Source Finder Lists SOURCES := $(shell find . -regex $(SOURCE_REGEX)) HEADERS := $(shell find . -regex $(HEADER_REGEX)) OBJECTS := $(shell echo $(SOURCES) | sed $(OBJECT_REGEX)) # COMMANDS RM := rm -vf TAGS := etags .PHONY : all exe objects clean tags # RECIPES all : exe exe : $(TARGET_NAME) $(TARGET_NAME): $(OBJECTS) $(LINK) $(LDFLAGS) $(TARGET_ARCH) $^ $(LOADLIBS) $(LDLIBS) -o $@ objects: $(OBJECTS) $(DEPS): $(SOURCES) $(HEADERS) $(CXX) $(INCPATHS) $(DEP_FLAG) $(SOURCES) > $(DEPS) clean: @$(RM) $(TARGET) @$(RM) $(OBJECTS) @$(RM) *.d @$(RM) *~ core @$(RM) $(DEPS) tags: $(HEADERS) $(SOURCES) $(TAGS) $(HEADERS) $(SOURCES) -include $(DEPS) ################################################################################ # On adding additional recipes: # I personally consult `make -p` to view a list of built-in rules that can # help strictly define what I need to. This will only work properly in a # directory that does not have a makefile in it. # # Example: # Suppose I want to add .yy support (bison files) in my generic makefile. # I need to add the suffix to the list of suffixes that already have # built-in rules. I need to define that for a .yy nothing needs to be done, # then I need to issue the command to invoke bison (yacc) to generate the # cpp file from the bison input file. # # .SUFFIXES += ,.yy # SOURCE_EXT += ,.yy # %.yy: # %.cpp: %.yy # $(YACC) $(YFLAGS) $< -o $@ #