Makefileã§ã½ã¼ã¹ãããããã¡ã¤ã«ã®ä¾åé¢ä¿ãå¦ç
大è¦æ¨¡ãªCè¨èªããã°ã©ã ã§ã¯ãã½ã¼ã¹ãããããã¡ã¤ã«ãè¤éãªä¾åé¢ä¿ãæã¤ãããããããèªå解決ãã¦ãããMakefileã欲ãããªãã 欲æãå ·ä½åããã¨
- ã½ã¼ã¹ããããããªãã¸ã§ã¯ããã¡ã¤ã«ã¯ç°ãªããã£ã¬ã¯ããªã«å ¥ããã
- ä¾åé¢ä¿ãèªèããæ´æ°ãã¹ããªãã¸ã§ã¯ããã¡ã¤ã«ãèªåæ¤åº
- æ°ããã½ã¼ã¹ãããããã¡ã¤ã«ãä½æããéãMakefileãæ¸ãæãã«æ¸ã
- ä»ã®ããã¸ã§ã¯ãã«ã使ãã¾ãããæ±ç¨æ§
- Makefileèªä½ãçããä¿å®ãããã
ã ãã¶è´ æ²¢ã ããé å¼µã£ã¦Makefileããããããã®ã§ãå¾ãããç¥è¦ãå¿ããªããã¡ã«ã¾ã¨ãã¦ããã 以åæ¸ããMakefileææ³ãããã ãåèã«ãã¦ããã
ãã£ã¬ã¯ããªæ§æ
root +----src/ | +---- main.c # mA.h ãèªã¿è¾¼ã | +---- A.c # mA.h 㨠A.h ãèªã¿è¾¼ã | +----inc/ | +---- mA.h # ä¾åãªã | +---- A.h # B.h ãèªã¿è¾¼ã | +---- B.h # ä¾åãªã | +----obj/ | +---- main.o # makeã³ãã³ãã§ä½æããã | +---- A.o # makeã³ãã³ãã§ä½æããã | +---- Makefile # *.o ãä½æ | +---- exec.out # makeã³ãã³ãã§ä½æããã +---- Makefile # exec.out ãä½æ
*.c *.h *.o ã¯ããããå¥ãã£ã¬ã¯ããªã§ç®¡çããã ãã¡ããä¾åé¢ä¿ãèæ ®ãã¦
- A.c ãæ´æ°ãããã°ãA.o ãæ´æ°
- mA.h ãæ´æ°ãããã°ãmain.o 㨠A.o ãæ´æ°
- B.h ãæ´æ°ãããã°ãA.o ãæ´æ°
ã¨ãã£ãå ·åã§ãªãã¸ã§ã¯ããã¡ã¤ã«ãä½ãç´ããexec.out ã«åãªã³ã¯ãããã®ã ã
Makefile
ãã®ç¶æ³ã«å¯¾å¦ããã«ã¯ãã¾ã root/Makefile ãä½ã
# root/Makefile CC := gcc ALL_C := $(wildcard src/*.c) # src/main.c src/A.c ALL_O := $(patsubst src/%.c,obj/%.o,$(ALL_C)) # obj/main.o obj/A.o ALL_CH := $(wildcard src/*.c inc/*.h) # src/*.c inc/*.h exec.out: $(ALL_CH) cd obj && $(MAKE) "CC=$(CC)" # obj/Makefile ãå®è¡ãã (ALL_Oãä½æããã) $(CC) $(ALL_O) -o $@ .PHONY: clean clean: @rm -rf *.out obj/*.o obj/*.d
ãã®ãã¡ã¤ã«ã®å¦çå
容㯠Makefileææ³ãããã ãè¦ãã°ç解ã§ããã ããã
è¦ããã«ã½ã¼ã¹ãããããã¡ã¤ã«ã«æ´æ°ãããã° exec.out
ããªãã«ãããã®ã ãã
æ´æ°ãã¹ããªãã¸ã§ã¯ããã¡ã¤ã«ã®æ¤åºã¨åã³ã³ãã¤ã«ã¯ root/obj/Makefileã«ä¸¸æããã¦ããã
ãã® root/obj/Makefile ã¯
# root/obj/Makefile ALL_C := $(wildcard ../src/*.c) # ../src/main.c ../src/A.c ALL_O := $(patsubst ../src/%.c,%.o,$(ALL_C)) # main.o A.o ALL_D := $(patsubst ../src/%.c,%.d,$(ALL_C)) # main.d A.d ALL_H := $(wildcard ../inc/*.h) # ../inc/mA.h ../inc/A.h ../inc/B.h .PHONY: dummy # dummy ã¨ãããã¡ã¤ã«ã¯ä½æãããªãã®ã§ PHONY æå® dummy: $(ALL_O) # å®è¡ããã«ã¯ ALL_O ãå¿ è¦ --> ä¸ã® %.o:... ã§ä½æ %.o: ../src/%.c # A.c 以å¤ã®ä¾åãã¡ã¤ã«ã¯ãä¸ã® -include A.d ã§è¨å®ããã $(CC) -c $< -o $@ %.d: ../src/%.c $(ALL_H) # ä¸ã® -include å½ä»¤ããå¼ã°ãã cpp -MM $< -MF $@ # A.c ã®ä¾åé¢ä¿ãMakefileå½¢å¼ã§æ¸ãã A.d ãçæ -include $(ALL_D) # main.d 㨠A.d ãèªã¿è¾¼ã (ç¡ãå ´å㯠%.d:... ã§ä½æ)
ãã¡ã¤ã«ã®åé ã® ALL_C
㨠ALL_O
ã¯æå³ããããã¨æãããALL_D
ã«ã¤ãã¦ã¯èª¬æãå¿
è¦ã ããã
å®ã¯ gcc ã«ã¯ããªããã»ããµã® cpp ãå«ã¾ãã¦ããã
$ cpp -MM A.c -MF A.d
ã¨ãããã¨ã§ A.c ãåç §ãã¦ããä»ã®ã½ã¼ã¹ãããããã¡ã¤ã«ã®æ å ±ã Makefile å½¢å¼ã§åå¾ã§ããã A.d ã®ä¸èº«ã¯ä»¥ä¸ã®ãããªæã
A.o: ../src/A.c ../inc/mA.h ../inc/A.h ../inc/B.h
ãããè¦ãã°ãA.d ãèªã¿è¾¼ã Makefile 㯠A.d ã¨åããã£ã¬ã¯ããªã«è¨ç½®ããå¿ è¦ãããã¨åããã ããã ããããäºæ 㧠root/Makefile ãã root/obj/Makefile ãåé¢ããã
cpp ãã¦å¾ããã A.d ããã¡ã¤ã«æ«å°¾ã® -include $(ALL_D)
ã«ããèªã¿è¾¼ãã°ã
æ¢ã«å®ç¾©ããã¦ãããã¿ã¼ã³ãããã以ä¸ã®ããã«ä¸æ¸ãããã
# ä¸æ¸ãå %.o: ../src/%.c $(CC) -c $< -o $@ # ä¸æ¸ãå¾ A.o: ../src/A.c ../inc/mA.h ../inc/A.h ../inc/B.h $(CC) -c $< -o $@
ããã§ãªãã¸ã§ã¯ããã¡ã¤ã«ã®ä¾åé¢ä¿ã解決ããæ£ããåã³ã³ãã¤ã«ã§ããããã«ãªã£ãããã ã
ã¾ã¨ã
make ã³ãã³ããå©ããå¾ã®å¦çå 容ãã¾ã¨ãã¦ããã
- root ãã£ã¬ã¯ããªã§ make ã³ãã³ããå©ãã
- root/Makefile ã®æåã®ãã«ãå½ä»¤
exec.out: $(ALL_CH)
ã«åºã¥ããä»»æã®ã½ã¼ã¹ã»ããããã¡ã¤ã«ãå¤åãã¦ããã° exec.out ãæ´æ°ãããã¨ããã cd obj && $(MAKE)
ã«ãããroot/obj/Makefile ã® make ãå®è¡ã- root/obj/Makefile ã®æçµè¡
-include $(ALL_D)
ã«ãããA.d 㨠main.d ãèªã¿è¾¼ããã¨ããã %.d: ../src/%.c $(ALL_H)
ã«ããã対象ã®ã½ã¼ã¹ãä»»æã®ããããã¡ã¤ã«ãå¤åãã¦ããã° A.d 㨠main.d ãæ´æ°ã- A.d 㨠main.d ãèªã¿è¾¼ãã
- ãã¿ã¼ã³ãããã®
%.o: ../src/%.c
ã A.d 㨠main.d ã«æ¸ãããä¾åé¢ä¿ã§ä¸æ¸ããããã - root/obj/Makefile ã®æåã®ãã«ãå½ä»¤
dummy: $(ALL_O)
ãå®è¡ã - dummyã®å¦çã¯ä½ããªãããä¾åãã¡ã¤ã«ã¨ã㦠main.o 㨠A.o ãæå®ããã¦ããã®ã§ãããããæ´æ°ãããã¨ããã
- (7ã§ä¸æ¸ããã)ä¾åé¢ä¿ã«åºã¥ãã¦ãmain.o 㨠A.o ãæ´æ°ããå¿ è¦ããããå¤æã
- å¿
è¦ã¨å¤æãããã°
$(CC) -c $< -o $@
ãå®è¡ãã¦ãmain.o 㨠A.o ãæ´æ°ã - dummy ã®å¦ç(ä½ãããªã)ãçµãã£ãã®ã§ãroot/Makefile ã«æ»ãã
$(CC) $(ALL_O) -o $@
ãå®è¡ããexec.out ãæ´æ°ã
ãã¼ããããããã å¦çé åºã¨ãã¦ã¯ 1,2,3 ã®å¾ã« 8,9,10,… ã¨ãããããã ãã8ãè¡ãåã« root/obj/Makefile ã®äºåæºåã¨ã㦠4,5,6,7 ãå®è¡ãããæãã ãªã