12 January 2013
版权声明:本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名elloop(包含链接)

一个多目标的GNU Makefile的示例

#===============================================================================
#
#         File: Makefile
#  Description: 
#
#        Usage: make              (generate executable(s)                    )
#               make clean        (remove objects, executables, prerequisits )
#               make tarball      (generate compressed archive               )
#               make zip          (generate compressed archive               )
#                                  
#       Author: Dr.-Ing. Fritz Mehner
#        Email: mehner@mfh-iserlohn.de
#      Created: 
#
#===============================================================================


CC              = gcc
CCP             = g++
CFLAGS          = -c -g -Wall
LFLAGS          = -g
SYS_LIBS        = -lm
TARBALL_EXCLUDE = "*.{o,gz,zip}"
ZIP_EXCLUDE     = *.o *.gz *.zip

TARGETS	= target_1 target_2

#---------- targets --------------------------------------
all:	$(TARGETS)

%.o:	%.c
			$(CC) $(CFLAGS) $*.c

%.o:	%.cc
			$(CCP) $(CFLAGS) $*.cc
 
#---------- target 1 -------------------------------------
#  C  target
target_1:	target_1.o
					$(CC) $(LFLAGS) -o $@ $@.o  $(SYS_LIBS)

#---------- target 2 -------------------------------------
# C++ target
target_2:	target_2.o
					$(CCP) $(LFLAGS) -o $@ $@.o  $(SYS_LIBS)


#---------- target 3 -------------------------------------



#---------- tarball --------------------------------------
tarball:
					lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
					rm --force $$lokaldir.tar.gz;              \
					tar --exclude=$(TARBALL_EXCLUDE)           \
					    --create                               \
					    --gzip                                 \
					    --verbose                              \
					    --file  $$lokaldir.tar.gz *

#---------- zip ------------------------------------------
zip:
					lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
					zip -r  $$lokaldir.zip * -x $(ZIP_EXCLUDE)

#---------- clear up -------------------------------------
clean:
				rm  --force  $(EXECUTABLE) $(OBJECTS) $(PREREQUISITES)


分享到