Makefile模板
Makefile模板:
1.生成可执行程序的Makefile
1 ######################################## 2 # 3 # build exectable object Makefile 4 ######################################## 5 SOURCE := $(wildcard *.c) $(wildcard *.cpp) 6 OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) 7 8 # edit the following variables 9 TARGET := test 10 CC := 11 LIBS := 12 LDFLAGS := 13 DEFINES := 14 INCLUDE := 15 16 CFLAGS := -Wall $(DEFINES) $(INCLUDE) 17 CXXFLAGS:= $(CFLAGS) 18 19 20 # do not change the following objects below before you understand it's use 21 .PHONY : everything objs rebuild clean veryclean 22 23 everything : $(TARGET) 24 25 objs : $(OBJS) 26 27 rebuild: veryclean everything 28 29 clean : 30 rm -fr *.o 31 32 veryclean : clean 33 rm -fr $(TARGET) 34 35 $(TARGET) : $(OBJS) 36 $(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
2.生成静态链接库的Makefile
1 ######################################## 2 # 3 # build static library Makefile 4 ######################################## 5 SOURCE := $(wildcard *.c) $(wildcard *.cpp) 6 OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) 7 8 # edit the following variables 9 TARGET := libtest.a 10 CC := 11 AR := 12 LIBS := 13 LDFLAGS := 14 DEFINES := 15 INCLUDE := 16 17 RANLIB := ranlib 18 CFLAGS := -Wall $(DEFINES) $(INCLUDE) 19 CXXFLAGS:= $(CFLAGS) 20 21 22 # do not change the following objects below before you understand it's use 23 .PHONY : everything objs rebuild clean veryclean 24 25 everything : $(TARGET) 26 27 objs : $(OBJS) 28 29 rebuild: veryclean everything 30 31 clean : 32 rm -fr *.o 33 34 veryclean : clean 35 rm -fr $(TARGET) 36 37 $(TARGET) : $(OBJS) 38 $(AR) cr $(TARGET) $(OBJS) 39 $(RANLIB) $(TARGET)
3.生成动态链接库的Makefile
1 ######################################## 2 # 3 # build dynamic library Makefile 4 ######################################## 5 SOURCE := $(wildcard *.c) $(wildcard *.cpp) 6 OBJS := $(SOURCE) 7 8 # edit the following variables 9 TARGET := libtest.so 10 CC := 11 LIBS := 12 LDFLAGS := 13 DEFINES := 14 INCLUDE := 15 16 CFLAGS := -Wall $(DEFINES) $(INCLUDE) 17 CXXFLAGS:= $(CFLAGS) 18 SHARE := -fPIC -shared -o 19 20 21 # do not change the following objects below before you understand it's use 22 .PHONY : everything objs rebuild clean veryclean 23 24 everything : $(TARGET) 25 26 objs : $(OBJS) 27 28 rebuild: veryclean everything 29 30 clean : 31 rm -fr *.o 32 33 veryclean : clean 34 rm -fr $(TARGET) 35 36 $(TARGET) : $(OBJS) 37 $(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)
作者:Alvin2012