linux »·¾³ÏµijÌÐòÔ±Èç¹û²»»áʹÓÃGNU makeÀ´¹¹½¨ºÍ¹ÜÀí×Ô¼ºµÄ¹¤³Ì£¬Ó¦¸Ã²»ÄÜËãÊÇÒ»¸öºÏ¸ñµÄרҵ³ÌÐòÔ±£¬ÖÁÉÙ²»ÄܳƵÃÉÏÊÇunix³ÌÐòÔ±¡£ÔÚlinux»·¾³ÏÂʹÓÃGNUµÄmake¹¤¾ßÄܹ»±È½ÏÈÝÒ׵Ĺ¹½¨Ò»¸öÊôÓÚÄã×Ô¼ºµÄ¹¤³Ì£¬Õû¸ö¹¤³ÌµÄ±àÒëÖ»ÐèÒªÒ»¸öÃüÁî¾Í¿ÉÒÔÍê³É±àÒ룬Á´½ÓÒÔÖÁÓÚ×îºóµÄÖ´ÐУ¬²»¹ýÕâÐèÒªÎÒÃÇͶÈëһЩʱ¼äÈ¥Íê³ÉÒ»¸ö»òÕ߶à¸ö³ÆΪmakefileÎļþµÄ±àд£¬´ÎÎļþÕýÊÇmakeÕý³£¹¤×÷µÄ»ù´¡¡£ËùÒªÍê³ÉµÄmakefileÎļþÃèÊöÁËÕû¸ö¹¤³ÌµÄ±àÒ룬Á´½ÓµÈ¹æÔò¡£
²âÊÔ³ÌÐòÔÚͬһ¸öÎļþÖУ¬¹²ÓÐ:book.c main.c menu.c Èý¸öÎļþ£¬Makefileд·¨ÈçÏÂËùʾ£º
sss:book.o main.o menu.o gcc -o sss book.o main.o menu.o
book.o:book.c book.h gcc -c book.c
main.o:main.c gcc -c main.c
menu.o:menu.c menu.h gcc -c menu.c
clean: rm -rf *o sss |
Ö´Ðйý³ÌÈçÏ£º
[root@localhost book]# make
gcc -c book.c
gcc -c main.c -I include
make -C menu
make[1]: Entering directory `/mnt/hgfs/mnt/20180910/book/menu'
gcc -c menu.c -o ../menu.o -I ../include
make[1]: Leaving directory `/mnt/hgfs/mnt/20180910/book/menu'
gcc -o sss book.o main.o menu.o
ͨÓÃÄ£°å
ÔÚʵ¼Êµ±ÖгÌÐòÎļþ±È½Ï´ó£¬Õâʱºò¶ÔÎļþ½øÐзÖÀ࣬·ÖΪͷÎļþ¡¢Ô´Îļþ¡¢Ä¿±êÎļþ¡¢¿ÉÖ´ÐÐÎļþ¡£Ò²¾ÍÊÇ˵ͨ³£½«Îļþ°´ÕÕÎļþÀàÐÍ·ÅÔÚ²»Í¬µÄĿ¼µ±ÖУ¬Õâ¸öʱºòµÄMakefileÐèҪͳһ¹ÜÀíÕâЩÎļþ£¬½«Éú²úµÄÄ¿±êÎļþ·ÅÔÚÄ¿±êĿ¼Ï£¬¿ÉÖ´ÐÐÎļþ·Åµ½¿ÉÖ´ÐÐĿ¼Ï¡£
DIR_INC = ./include DIR_SRC = ./src DIR_OBJ = ./obj DIR_BIN = ./bin
SRC = $(wildcard ${DIR_SRC}/*.c) OBJ = $(patsubst %.c,${DIR_OBJ}/%.o,$(notdir ${SRC}))
TARGET = main
BIN_TARGET = ${DIR_BIN}/${TARGET}
CC = gcc CFLAGS = -g -Wall -I${DIR_INC}
${BIN_TARGET}:${OBJ} $(CC) $(OBJ) -o $@
${DIR_OBJ}/%.o:${DIR_SRC}/%.c $(CC) $(CFLAGS) -c $< -o $@ .PHONY:clean clean: find ${DIR_OBJ} -name *.o -exec rm -rf {} |
´úÂë½âÊÍÈçÏ£º
£¨1£©MakefileÖеķûºÅ $@, $^, $< µÄÒâ˼£º
¡¡¡¡$@ ±íʾĿ±êÎļþ
¡¡¡¡$^ ±íʾËùÓеÄÒÀÀµÎļþ
¡¡¡¡$< ±íʾµÚÒ»¸öÒÀÀµÎļþ
¡¡¡¡$? ±íʾ±ÈÄ¿±ê»¹ÒªÐµÄÒÀÀµÎļþÁбí
£¨2£©wildcard¡¢notdir¡¢patsubstµÄÒâ˼£º
¡¡¡¡wildcard : À©Õ¹Í¨Åä·û
¡¡¡¡notdir £ºÈ¥³ý·¾¶
¡¡¡¡patsubst £ºÌ滻ͨÅä·û
SRC = $(wildcard *.c)
µÈÓÚÖ¸¶¨±àÒ뵱ǰĿ¼ÏÂËùÓÐ.cÎļþ£¬Èç¹û»¹ÓÐ×ÓĿ¼£¬±ÈÈç×ÓĿ¼Ϊinc£¬ÔòÔÙÔö¼ÓÒ»¸öwildcardº¯Êý£¬ÏóÕâÑù£º
SRC = $(wildcard *.c) $(wildcard inc/*.c)