標籤

C++ (12) Linux (6) MacOSX (4) Makefile (3) Matlab (3) Ubuntu (3) Android (2) C (1) Refactoring (1)

2013年3月15日 星期五

[Makefile] Rules & Implicit Rules

%.o : %.cpp
        $(CC) -o ...

這種寫法只有"當前目錄下"的.o檔才會使用這條rule
如果要把*.o放到obj/下的話
就得寫成
obj/%.o: %.cpp
        $(CC) -o ...
這樣才找得到

另外可以在terminal下輸入make -p
來看到整個make的流程

[Makefile] add dependency

target1: d1
        $(CC) -o ....

target1: d2 d3
target1: d4 d5

這樣寫的話
後面兩個都沒有實際執行任何東西
所以第一行會被解讀成
target1: d1 d2 d3 d4 d5

2013年3月14日 星期四

[Makefile] CFLAGS & CPPFLAGS

轉自 http://stackoverflow.com/questions/2754966/cflags-vs-cppflags
其他參考資料 http://wiki.ubuntu.org.cn/index.php?title=%E8%B7%9F%E6%88%91%E4%B8%80%E8%B5%B7%E5%86%99Makefile:MakeFile%E4%BB%8B%E7%BB%8D&variant=zh-hant
http://www.makelinux.net/make3/make3-CHP-2-SECT-7

Makefile 在沒有pattern rules時
會自動使用內建的implicit rules去compile *.c 和 *.cpp


%.o:%.c
    $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<


%.o:%.cpp
    $(CC) $(CPPFLAGS) -c -o $@ $<

CPPFLAGS 和 CFLAGS 的差別在於
如果檔案是*.cpp的形式
內建的implicit rule並不會去使用到CFLAGS
只會用到CPPFLAGS
所以如果把 -g -w -I include/這些東西放到CFLAGS的話
是沒有用的