码迷,mamicode.com
首页 > 其他好文 > 详细

(代码分享) makefile模板

时间:2015-01-30 16:46:47      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

.PHONY: clean all

FILETYPE = c
# what language is used: c  or  cpp
TARGETS = program
#target name
TREE = 0
# whether the source files are organised in tree or not
# (source files are divided into ‘inc‘ ‘src‘ ‘lib‘ directories):
# 0 not; 1 yes

ifeq ($(strip $(FILETYPE)), c)
CC = gcc
else
CC = g++
endif

MKDIR = mkdir
RM = rm
RMFLAGS = -rf
CFLAGS = -Wall -W -g 
LDLIBS = 
# libraries to link to ,default not

DIR = $(shell pwd)

ifeq ($(strip $(TREE)), 0)
SRC_DIR = $(DIR)
LIB_DIR = $(DIR)
INC_DIR = $(DIR)
else
SRC_DIR = $(DIR)/src
LIB_DIR = $(DIR)/lib
INC_DIR = $(DIR)/inc
endif

DEPS_DIR = $(DIR)/.deps
OBJ_DIR = $(DIR)/.obj
DIRS = $(DEPS_DIR) $(OBJ_DIR) 
RMS = $(DIRS)

INCLUDE = -I$(INC_DIR)
SRCS = $(wildcard $(SRC_DIR)/*.$(FILETYPE))
OBJS = $(patsubst $(SRC_DIR)/%.$(FILETYPE), $(OBJ_DIR)/%.o, $(SRCS))
DEPS = $(patsubst $(OBJ_DIR)/%.o, $(DEPS_DIR)/%.dep, $(OBJS))
RMS += $(TARGETS) $(OBJS)


all: $(TARGETS) $(DEPS)

ifneq ($(MAKECMDGOALS), clean)
-include $(DEPS)
endif

$(DIRS):
    $(MKDIR) $(DIRS)
    
$(TARGETS):$(OBJS)
    $(CC) $(INCLUDE) -L$(LIB_DIR) -o $@ $(filter %.o, $^) $(LDLIBS)

$(OBJ_DIR)/%.o:$(OBJ_DIR) $(SRC_DIR)/%.$(FILETYPE)
    $(CC) -o $@ -c $(filter %.$(FILETYPE), $^) $(CFLAGS) $(INCLUDE) 

$(DEPS_DIR)/%.dep:$(SRC_DIR)/%.$(FILETYPE) $(DEPS_DIR) 
    $(CC) -MM $(INCLUDE) $(CFLAGS) $< | sed -e s,^\(.*\.o\),$(OBJ_DIR)/\1, > $@


clean:
    $(RM) $(RMFLAGS) $(RMS).PHONY: clean all

 

(代码分享) makefile模板

标签:

原文地址:http://www.cnblogs.com/venus024/p/4262331.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!