Makefile tổng quát

Hôm qua lọ mọ viết một Makefile cho sinh viên cho các bài tập lập trình học kỳ tới. Makefile này khá tổng quát cho các chương trình cỡ chục nghìn dòng lệnh đổ lại. Nó giả sử là mỗi .c đều có .h tương ứng. Dễ dàng sửa lại một chút để nó dịch các chương trình C++. Ghi lại đây biết đâu lại có lợi cho ai đó.

#############################################################################
#   $ make           compile and link all C programs specified with PROGS
#   $ make clean     clean objects and the executable file
#   $ make distclean clean objects, the executable and dependencies
# I assume that each .c has a .h of the same name in directory INC_DIR
# You can compile multiple programs by redefining $(PROGS)
#===========================================================================

## Customizable Section: adapt those variables to suit your program.
##==========================================================================
# The pre-processor and compiler options.
MY_CFLAGS = -I./include -DBE_DEBUG -D_REENTRANT -Wall -D__EXTENSIONS__

# Libraries to link to, depending on OS
UNAME = $(shell uname)
ifeq ($(UNAME), SunOS)              # Sun OS
MY_LIBS = -lresolv -lsocket -lnsl
endif
ifeq ($(UNAME), Linux)              # Linux
MY_LIBS = -lresolv -lnsl
endif
ifeq ($(UNAME), Darwin)             # Mac OS
MY_LIBS =
endif

# Directory where your .h files are
INC_DIR := ./include

# The options used in linking as well as in any direct use of ld.
LDFLAGS +=

# The executable file names.
PROGS   = server client iom_client

## Implicit Section: change the following only when necessary.
##==========================================================================

CC := gcc
SRCS := $(wildcard *.c)

# objects other than those in PROGS
ALL_OBJS := ${SRCS:.c=.o}
ALL_NAMES := ${ALL_OBJS:.o=}

# all object files other than those named $(PROGS).o
COMMON_OBJS :=
define APPEND_CO
ifeq (,$(findstring $(1), $(PROGS)))
COMMON_OBJS += $(1).o
endif
endef
$(foreach name,$(ALL_NAMES),$(eval $(call APPEND_CO, $(name))))

.PHONY: all clean distclean

all: $(PROGS)

define PROG_RULE
$(1) : $(COMMON_OBJS) $(1).o
        ${CC} -I${INC_DIR} ${CFLAGS} ${MY_LIBS} ${COMMON_OBJS} $(1).c -o $(1)
endef

$(foreach program,$(PROGS),$(eval $(call PROG_RULE, $(program))))

## Objects are dependent on headers (and implicitly .c files)
##==========================================================================
define OBJ_RULE
$(1) : ${INC_DIR}/${1:.o=.h} ${1:.o=.c}
        ${CC} -I${INC_DIR} ${CFLAGS} -c ${1:.o=.c}
endef

$(foreach obj_file,$(ALL_OBJS),$(eval $(call OBJ_RULE,$(obj_file))))

## clean things up
##==========================================================================
clean:
        @- $(RM) $(PROGS)
        @- $(RM) $(ALL_OBJS)

distclean: clean

Chủ đề : Lập trình, Mạng máy tính and tagged . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

2 Comments

  1. luong
    Posted 22/08/2010 at 10:54 am | Permalink

    Nói chuyện Makfile. Hôm thứ sáu tôi ngồi ba tiếng đồng hồ kiếm một cái compilation error, mà khi kiếm xong tốn 5 giây để sửa. Số là mỗi lần run “make all” đều bị lổi, tôi thì biết chắc lổi ở đâu rồi nên không thèm nhìn cái error message. Nên loai quoai mãi mà “make all” vẫn xì nẹt với mình. Cuối cùng nhìn kỷ lại cái error message thì nó chỉ rõ cho mình rồi, chỉ vì mình có định kiến nên nó ngay trước mặt mà không chịu nhìn. Mà mình đâu là phải sinh viên mới ra trường.

    Tại sau có những người như Ngô Bão Châu, Lê Quang Liêm (19 tuổi. Sẽ là người VN đầu tiên được vô club 2700+ cũa cờ vua — super-grandmaster), và lại có những người như thằng Lương này !!

    Chiều về thất chí ôm vợ kể chuyện trong sở. Vợ nói một câu không gín giáng dì với câu chuyện — “Em lớn tuổi rồi, nhưng chắc là cũng còn vài người muốn được ôm em lúc này” rồi nhìn tôi cười mĩm như mấy ông thiền sư ra công án cho đệ tử. Tôi giậc mình đại ngộ — mình vẫn không chịu nhìn cái ngay trước mặt !!

  2. Posted 23/08/2010 at 3:56 am | Permalink

    Chuyện của anh rất dễ thương.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>