├── README.md └── Makefile /README.md: -------------------------------------------------------------------------------- 1 | # MakefileTemplate 2 | A Generic Makefile Template for C/C++ Projects 3 | 4 | ## Description 5 | 6 | ### Features: 7 | * Automatically traverses and lists all (code files in) sub-directories, or you manually add; 8 | * Automatically generates and processes dependencies; 9 | * Can hanlde cross-compiling or cross-platform projects (e.g. Linux, MacOS, Windows); 10 | * Can work with C projects, C++ projects, or mixed; 11 | * With examples in-file, and flags for high performance with small size of binary; 12 | * Not only for executable binaries, but also for static & dynamic libraries [to-do]. 13 | 14 | ### Usage: 15 | 1. Copy the `Makefile` file to your program source code directory. 16 | 2. Customize in the "Customizable Section" only if necessary: 17 | * to use non-standard C/C++ libraries, set pre-processor or compiler 18 | options to `` and linker ones to `` 19 | * to search sources in more directories, set to `` 20 | * to specify your favorite program name, set to `` 21 | 3. Type `make` to start building your program. 22 | 23 | ### Make Target: 24 | The Makefile provides the following targets to make: 25 | ```Shell 26 | make # compile and link 27 | make NODEP=yes # compile and link without generating dependencies 28 | make objs # compile only (no linking) 29 | make tags # create tags for Emacs editor 30 | make ctags # create ctags for VI editor 31 | make clean # clean objects and the executable file 32 | make distclean # clean objects, the executable and dependencies 33 | make help # get the usage of the makefile 34 | ``` 35 | 36 | ## Users 37 | + [Pear] (https://pear.hk) 38 | + [HKUST] (http://www.ust.hk) 39 | 40 | ## Reference & Thanks to 41 | + https://sourceforge.net/projects/gcmakefile/ 42 | + https://github.com/mbcrawfo/GenericMakefile 43 | + http://blog.csdn.net/kevin1078/article/details/7492619 44 | + http://www.digitalpeer.com/blog/example 45 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################# 2 | # 3 | # Generic Makefile Template for C/C++ Projects 4 | # 5 | # License: GPL (General Public License) 6 | # Note: GPL only applies to this file; you can use this Makefile in non-GPL projects. 7 | # Author: Pear 8 | # Date: 2016/04/26 (version 0.7) 9 | # Author: kevin1078 10 | # Date: 2012/04/24 (version 0.6) 11 | # Author: whyglinux 12 | # Date: 2008/04/05 (version 0.5) 13 | # 2007/06/26 (version 0.4) 14 | # 2007/04/09 (version 0.3) 15 | # 2007/03/24 (version 0.2) 16 | # 2006/03/04 (version 0.1) 17 | #=========================================================================== 18 | 19 | ## Customizable Section: adapt those variables to suit your program. 20 | ##========================================================================== 21 | 22 | # The executable file name. Must be specified. 23 | PROGRAM = zzz_pear_test 24 | 25 | # C and C++ program compilers. Un-comment and specify for cross-compiling if needed. 26 | #CC = gcc 27 | #CXX = g++ 28 | # Un-comment the following line to compile C programs as C++ ones. 29 | #CC = $(CXX) 30 | 31 | # The extra pre-processor and compiler options; applies to both C and C++ compiling as well as LD. 32 | EXTRA_CFLAGS = -fdata-sections -ffunction-sections 33 | 34 | # The extra linker options, e.g. "-lmysqlclient -lz" 35 | EXTRA_LDFLAGS = 36 | 37 | # Specify the include dirs, e.g. "-I/usr/include/mysql -I./include -I/usr/include -I/usr/local/include". 38 | INCLUDE = 39 | 40 | # The C Preprocessor options (notice here "CPP" does not mean "C++"; man cpp for more info.). Actually $(INCLUDE) is included. 41 | CPPFLAGS = -Wall -Wextra # helpful for writing better code (behavior-related) 42 | 43 | # The options used in linking as well as in any direct use of ld. 44 | LDFLAGS = 45 | 46 | # The directories in which source files reside. 47 | # If not specified, all subdirectories of the current directory will be added recursively. 48 | SRCDIRS := 49 | 50 | # OS specific. 51 | EXTRA_CFLAGS_MACOS = 52 | EXTRA_LDFLAGS_MACOS = -Wl,-search_paths_first -Wl,-dead_strip -v # deleting unused code for Pear, for minimal exe size 53 | LDFLAGS_MACOS = 54 | EXTRA_CFLAGS_LINUX = 55 | EXTRA_LDFLAGS_LINUX = -Wl,--gc-sections -Wl,--strip-all # deleting unused code for Pear, for minimal exe size 56 | LDFLAGS_LINUX = 57 | EXTRA_CFLAGS_WINDOWS = 58 | EXTRA_LDFLAGS_WINDOWS = 59 | LDFLAGS_WINDOWS = 60 | 61 | # Actually process the OS specific flags. 62 | UNAME_S := $(shell uname -s) 63 | ifeq ($(UNAME_S), Darwin) # if MacOS 64 | EXTRA_CFLAGS += $(EXTRA_CFLAGS_MACOS) 65 | EXTRA_LDFLAGS += $(EXTRA_LDFLAGS_MACOS) 66 | LDFLAGS += $(LDFLAGS_MACOS) 67 | else ifeq ($(UNAME_S), Linux) # if Linux 68 | EXTRA_CFLAGS += $(EXTRA_CFLAGS_LINUX) 69 | EXTRA_LDFLAGS += $(EXTRA_LDFLAGS_LINUX) 70 | LDFLAGS += $(LDFLAGS_LINUX) 71 | else # Windows, or... need to specify "MINGW" or "CYGWIN" to correctly detect. 72 | EXTRA_CFLAGS += $(EXTRA_CFLAGS_WINDOWS) 73 | EXTRA_LDFLAGS += $(EXTRA_LDFLAGS_WINDOWS) 74 | LDFLAGS += $(LDFLAGS_WINDOWS) 75 | endif 76 | 77 | #Actually $(INCLUDE) is included in $(CPPFLAGS). 78 | CPPFLAGS += $(INCLUDE) 79 | 80 | ## Implicit Section: change the following only when necessary. 81 | ##========================================================================== 82 | 83 | # The source file types (headers excluded). 84 | # .c indicates C source files, and others C++ ones. 85 | SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp 86 | 87 | # The header file types. 88 | HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp 89 | 90 | # The pre-processor and compiler options. 91 | # Users can override those variables from the command line. 92 | CFLAGS = -O3 93 | CXXFLAGS= -O3 94 | 95 | # The command used to delete file. 96 | RM = rm -f 97 | 98 | ETAGS = etags 99 | ETAGSFLAGS = 100 | 101 | CTAGS = ctags 102 | CTAGSFLAGS = 103 | 104 | ## Stable Section: usually no need to be changed. But you can add more. 105 | ##========================================================================== 106 | ifeq ($(SRCDIRS),) 107 | SRCDIRS := $(shell find $(SRCDIRS) -type d) 108 | endif 109 | SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS)))) 110 | HEADERS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(HDREXTS)))) 111 | SRC_CXX = $(filter-out %.c,$(SOURCES)) 112 | OBJS = $(addsuffix .o, $(basename $(SOURCES))) 113 | #DEPS = $(OBJS:%.o=%.d) #replace %.d with .%.d (hide dependency files) 114 | DEPS = $(foreach f, $(OBJS), $(addprefix $(dir $(f))., $(patsubst %.o, %.d, $(notdir $(f))))) 115 | 116 | ## Define some useful variables. 117 | DEP_OPT = $(shell if `$(CC) --version | grep -i "GCC" >/dev/null`; then \ 118 | echo "-MM"; else echo "-M"; fi ) 119 | DEPEND.d = $(CC) $(DEP_OPT) $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) 120 | COMPILE.c = $(CC) $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) -c 121 | COMPILE.cxx = $(CXX) $(EXTRA_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c 122 | LINK.c = $(CC) $(EXTRA_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) 123 | LINK.cxx = $(CXX) $(EXTRA_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) 124 | 125 | .PHONY: all objs tags ctags clean distclean help show 126 | 127 | # Delete the default suffixes 128 | .SUFFIXES: 129 | 130 | all: $(PROGRAM) 131 | 132 | # Rules for creating dependency files (.d). 133 | #------------------------------------------ 134 | 135 | .%.d:%.c 136 | @echo -n $(dir $<) > $@ 137 | @$(DEPEND.d) $< >> $@ 138 | 139 | .%.d:%.C 140 | @echo -n $(dir $<) > $@ 141 | @$(DEPEND.d) $< >> $@ 142 | 143 | .%.d:%.cc 144 | @echo -n $(dir $<) > $@ 145 | @$(DEPEND.d) $< >> $@ 146 | 147 | .%.d:%.cpp 148 | @echo -n $(dir $<) > $@ 149 | @$(DEPEND.d) $< >> $@ 150 | 151 | .%.d:%.CPP 152 | @echo -n $(dir $<) > $@ 153 | @$(DEPEND.d) $< >> $@ 154 | 155 | .%.d:%.c++ 156 | @echo -n $(dir $<) > $@ 157 | @$(DEPEND.d) $< >> $@ 158 | 159 | .%.d:%.cp 160 | @echo -n $(dir $<) > $@ 161 | @$(DEPEND.d) $< >> $@ 162 | 163 | .%.d:%.cxx 164 | @echo -n $(dir $<) > $@ 165 | @$(DEPEND.d) $< >> $@ 166 | 167 | # Rules for generating object files (.o). 168 | #---------------------------------------- 169 | objs:$(OBJS) 170 | 171 | %.o:%.c 172 | $(COMPILE.c) $< -o $@ 173 | 174 | %.o:%.C 175 | $(COMPILE.cxx) $< -o $@ 176 | 177 | %.o:%.cc 178 | $(COMPILE.cxx) $< -o $@ 179 | 180 | %.o:%.cpp 181 | $(COMPILE.cxx) $< -o $@ 182 | 183 | %.o:%.CPP 184 | $(COMPILE.cxx) $< -o $@ 185 | 186 | %.o:%.c++ 187 | $(COMPILE.cxx) $< -o $@ 188 | 189 | %.o:%.cp 190 | $(COMPILE.cxx) $< -o $@ 191 | 192 | %.o:%.cxx 193 | $(COMPILE.cxx) $< -o $@ 194 | 195 | # Rules for generating the tags. 196 | #------------------------------------- 197 | tags: $(HEADERS) $(SOURCES) 198 | $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES) 199 | 200 | ctags: $(HEADERS) $(SOURCES) 201 | $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES) 202 | 203 | # Rules for generating the executable. 204 | #------------------------------------- 205 | $(PROGRAM):$(OBJS) 206 | ifeq ($(SRC_CXX),) # C program 207 | $(LINK.c) $(OBJS) $(EXTRA_LDFLAGS) -o $@ 208 | @echo Type ./$@ to execute the program. 209 | else # C++ program 210 | $(LINK.cxx) $(OBJS) $(EXTRA_LDFLAGS) -o $@ 211 | @echo Type ./$@ to execute the program. 212 | endif 213 | 214 | ifndef NODEP 215 | ifneq ($(DEPS),) 216 | sinclude $(DEPS) 217 | endif 218 | endif 219 | 220 | clean: 221 | $(RM) $(OBJS) $(PROGRAM) $(PROGRAM).exe 222 | 223 | distclean: clean 224 | $(RM) $(DEPS) TAGS 225 | 226 | # Show help. 227 | help: 228 | @echo "Pear's Generic Makefile for C/C++ Projects" 229 | @echo 'Copyright (C) 2016 Pear ' 230 | @echo 'Copyright (C) 2007, 2008 whyglinux ' 231 | @echo 232 | @echo 'Usage: make [TARGET]' 233 | @echo 'TARGETS:' 234 | @echo ' all (=make) compile and link.' 235 | @echo ' NODEP=yes make without generating dependencies.' 236 | @echo ' objs compile only (no linking).' 237 | @echo ' tags create tags for Emacs editor.' 238 | @echo ' ctags create ctags for VI editor.' 239 | @echo ' clean clean objects and the executable file.' 240 | @echo ' distclean clean objects, the executable and dependencies.' 241 | @echo ' show show variables (for debug use only).' 242 | @echo ' help print this message.' 243 | @echo 244 | @echo 'Report bugs to .' 245 | 246 | # Show variables (for debug use only.) 247 | show: 248 | @echo 'PROGRAM :' $(PROGRAM) 249 | @echo 'SRCDIRS :' $(SRCDIRS) 250 | @echo 'HEADERS :' $(HEADERS) 251 | @echo 'SOURCES :' $(SOURCES) 252 | @echo 'SRC_CXX :' $(SRC_CXX) 253 | @echo 'OBJS :' $(OBJS) 254 | @echo 'DEPS :' $(DEPS) 255 | @echo 'DEPEND :' $(DEPEND) 256 | @echo 'DEPEND.d :' $(DEPEND.d) 257 | @echo 'COMPILE.c :' $(COMPILE.c) 258 | @echo 'COMPILE.cxx :' $(COMPILE.cxx) 259 | @echo 'link.c :' $(LINK.c) 260 | @echo 'link.cxx :' $(LINK.cxx) 261 | 262 | ## End of the Makefile ## Suggestions are welcome ## All rights reserved ## 263 | ############################################################################# 264 | --------------------------------------------------------------------------------