This paper divides makefile into three parts: generate makefile of executable file, generate makefile of static link library, generate makefile of dynamic link library.
These makefile are simple, they are usually used at a glance, and are easy to use, just copy them to the same directory as your code and then use make to generate the target files.
Here is the source code for the three makefile:
1. Makefile to generate executable files
##############################################################################source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) #target can change test to what do you want#target file name, enter any executable file name you wantTARGET: =Test#compile and lib parameter#Compile ParametersCC: =Gcclibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_h#I think you should does anything here#The following basically does not need to make any changes.. Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. So RM-FR *. O Veryclean:clean rm-FR $ (target) $ (target): $ (OBJS) $ (CC) $ (cxxflags)-O [email protected] $ (OBJS) $ (ldflags) $ (LIBS)
2, generate static link library makefile
############################################################################## #target can change test to what do you want#Shared library file name, lib*.aTARGET: =libtest.a#compile and lib parameter#Compile ParametersCC: =Gccar=Arranlib=Ranliblibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_h#I think you should does anything here#The following basically does not need to make any changes. #source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) . Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. O Veryclean:clean rm-Fr $ (target) $ (target) : $ (OBJS) $ (AR) Cru $ (target) $ (OBJS) $ (ranlib) $ (target)
3. Generate Makefile for dynamic link library
############################################################################## #target can change test to what do you want#Shared library file name, lib*.soTARGET: =libtest.so#compile and lib parameter#Compile ParametersCC: =Gcclibs:=Ldflags:=defines:=INCLUDE:= -I.cflags:=-g-wall-O3 $ (defines) $ (INCLUDE) Cxxflags:= $ (CFLAGS)-Dhave_config_hshare:=-fpic-shared-o#I think you should does anything here#The following basically does not need to make any changes. #source File#source files, automatically find all. C and. cpp files, and define the target as the same name. o FileSOURCE: = $ (wildcard *.c) $ (wildcard *. cpp) OBJS:= $ (Patsubst%.c,%.o,$ (Patsubst%.cpp,%. o,$ (SOURCE))) . Phony:everything Objs Clean Veryclean rebuild everything: $ (target) All: $ (target) OBJS: $ (OBJS) rebuild:verycle An everything clean:rm-FR *. O Veryclean:clean rm-FR $ (target) $ (target): $ (OBJS) $ (CC) $ (cxxflags) $ (SHARE) [email protected] $ (OBJS) $ (ldflags) $ (LIBS)
Makefile common universal templates (including static link library, dynamic link library, executable file)