話很少說,請看:網站
個人項目有的目錄結構有:spa
dirls/
├── include
│ └── apue.h
├── lib
│ ├── error.c
│ ├── error.o
│ └── Makefile
├── src
│ ├── dirls.c
│ ├── dirls.out
│ └── Makefile
└── test_client
而個人Makefile模板代碼以下:code
SRCS = $(wildcard *.c ../lib/*.c) #wildcard把 指定目錄 ./ 和 ../lib 下的全部後綴是c的文件所有展開。 OBJS = $(SRCS:.c = .o) #OBJS將$(SRCS)下的.c文件轉化爲.o文件 CC = gcc #表明所使用的編譯器 INCLUDES = -I../include \ #頭文件查找路徑 -I. \ LIBS = -L../lib \ #連接庫查找地址 CCFLAGS = -g -Wall -O0 #附加參數 OUTPUT = dirls.out #輸出程序名稱 all:$(OUTPUT) $(OUTPUT) : $(OBJS) $(CC) $^ -o $@ $(INCLUDES) $(LIBS) %.o : %.c $(CC) -c $< $(CCFLAGS) clean: rm -rf *.out *.o #清除中間文件及生成文件 .PHONY:clean
另外附上別的網站的幾個Makefile模板:blog
一、編譯動態庫get
############################################################# # Makefile for shared library. # 編譯動態連接庫 ############################################################# #set your own environment option CC = g++ CC_FLAG = -D_NOMNG -D_FILELINE #set your inc and lib INC = LIB = -lpthread -L./ -lsvrtool #make target lib and relevant obj PRG = libsvrtool.so OBJ = Log.o #all target all:$(PRG) $(PRG):$(OBJ) $(CC) -shared -o $@ $(OBJ) $(LIB) .SUFFIXES: .c .o .cpp .cpp.o: $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o .PRONY:clean clean: @echo "Removing linked and compiled files......; rm -f $(OBJ) $(PRG)
二、編譯靜態庫編譯器
############################################################# # Makefile for static library. # 編譯靜態連接庫 ############################################################# #set your own environment option CC = g++ CC_FLAG = -D_NOMNG -D_FILELINE #static library use 'ar' command AR = ar #set your inc and lib INC = LIB = -lpthread -L./ -lsvrtool #make target lib and relevant obj PRG = libsvrtool.a OBJ = Log.o #all target all:$(PRG) $(PRG):$(OBJ) ${AR} rv ${PRG} $? .SUFFIXES: .c .o .cpp .cpp.o: $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o .PRONY:clean clean: @echo "Removing linked and compiled files......" rm -f $(OBJ) $(PRG)
三、可執行程序io
########################################### #Makefile for simple programs ########################################### INC= LIB= -lpthread CC=CC CC_FLAG=-Wall PRG=threadpooltest OBJ=CThreadManage.o CThreadPool.o CThread.o CWorkerThread.o threadpooltest.o $(PRG):$(OBJ) $(CC) $(INC) $(LIB) -o $@ $(OBJ) .SUFFIXES: .c .o .cpp .cpp.o: $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o .PRONY:clean clean: @echo "Removing linked and compiled files......" rm -f $(OBJ) $(PRG)