For more information, see:
My project has the following directory structures:
dirls/├── include│ └── apue.h├── lib│ ├── error.c│ ├── error.o│ └── Makefile├── src│ ├── dirls.c│ ├── dirls.out│ └── Makefile└── test_client
My makefile template code is as follows:
SRCS = $ (wildcard *. C ../lib/*. c) # wildcard expands all files with the specified directory./And./lib suffixed with C. Objs = $ (SRCS :. C =. o) # objs will be under $ (SRCS. c file. o file cc = GCC # indicates the used compiler des =-I .. /include \ # header file search path-I. libs =-l .. /Lib \ # Link Library search address ccflags =-g-wall-O0 # additional parameter output = dirls. out # Name of the output program all: $ (output): $ (objs) $ (CC) $ ^-O [email protected] $ (DES) $ (libs) %. o: %. C $ (CC)-C $ <$ (ccflags) Clean: Rm-RF *. out *. O # clear intermediate files and generate files. phony: clean
Several makefile templates for other websites are attached:
1. Compile the dynamic library
######################################## ###################### Makefile for shared library. # compile a dynamic link library ################################### ########################## set your own environment optioncc = g ++ cc_flag =-d_nomng -d_fileline # Set Your INC and libinc = Lib =-lpthread-L. /-lsvrtool # Make target lib and relevant obj prg = libsvrtool. soobj = log. O # All targetall: $ (PRG): $ (OBJ) $ (CC)-shared-O [email protected] $ (OBJ) $ (LIB ). suffixes :. c. o. CPP. CPP. o: $ (CC) $ (cc_flag) $ (INC)-C $ *. CPP-o $ *. o. prony: cleanclean: @ echo "Removing linked and compiled files ......; rm-F $ (OBJ) $ (PRG)
2. Compile the static library
######################################## ###################### Makefile for static library. # compile a static Link Library ################################### ########################## set your own environment optioncc = g ++ cc_flag =-d_nomng -d_fileline # static library use 'ar 'command AR = ar # Set Your INC and libinc = Lib =-lpthread-L. /-lsvrtool # Make target lib and relevant obj prg = libsvrtool. aobj = log. O # All targe Tall: $ (PRG): $ (OBJ) $ {ar} RV $ {PRG} $ ?. Suffixes :. c. o. CPP. CPP. o: $ (CC) $ (cc_flag) $ (INC)-C $ *. CPP-o $ *. o. prony: cleanclean: @ echo "Removing linked and compiled files ...... "RM-F $ (OBJ) $ (PRG)
3. executable program
############################################Makefile for simple programs###########################################INC=LIB= -lpthreadCC=CCCC_FLAG=-WallPRG=threadpooltestOBJ=CThreadManage.o CThreadPool.o CThread.o CWorkerThread.o threadpooltest.o$(PRG):$(OBJ) $(CC) $(INC) $(LIB) -o [email protected] $(OBJ) .SUFFIXES: .c .o .cpp.cpp.o: $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o.PRONY:cleanclean: @echo "Removing linked and compiled files......" rm -f $(OBJ) $(PRG)
A general makefile template I use