1 / 21

Make

Make. WanChing Chou , Department of Mathematics, National Taiwan University 2009/02/21. Outline. 組成元素 變數 變數代換 Shell Makefile 變數 目標 命令 範例. 變數. 變數. 像寫程式一樣, make 規則裡面的組成可以有動態的值,這時就需要用變數來 設值來取代與轉換以及日後維護。 變數的規則 字母大小有差。 不要用字母數字底線以外的字元。可以有空格在前面後面。 使用變數用 $(VAR) 或者 ${VAR} 都可以。

astin
Download Presentation

Make

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Make WanChing Chou, Department of Mathematics, National Taiwan University2009/02/21

  2. Outline • 組成元素 • 變數 • 變數代換 • Shell Makefile 變數 • 目標 • 命令 • 範例

  3. 變數 Introduction to Make

  4. 變數 像寫程式一樣,make規則裡面的組成可以有動態的值,這時就需要用變數來 設值來取代與轉換以及日後維護。 變數的規則 字母大小有差。 不要用字母數字底線以外的字元。可以有空格在前面後面。 使用變數用$(VAR)或者${VAR}都可以。 如果要用$,請多加一個$變成$$,在Shell Command會用到Shell 變數此時就要加$。 Introduction to Make

  5. 變數 Introduction to Make 5 Example 設objects objects = program.o foo.o utils.o program : $(objects) cc -o program $(objects)

  6. 變數代換 Introduction to Make 6 遞迴的變數代換 var = value 範例: x = fooy = $(x) barx = xyz# y 的值為 xyz bar

  7. 變數代換 Introduction to Make 7 簡單的變數代換 var := value 範例: x := fooy := $(x) barx := xyz# y 的值為 foo bar

  8. Shell以及Makefile變數 Introduction to Make 8 傳Shell的變數給Shell就不要讓make對$這個符號做解釋, 所以要多加$。 linuxsubdirs: dummy set -e; for i in $(Subdirs); do $(MAKE) -C $$i; done

  9. Shell以及Makefile變數 Introduction to Make 9 傳變數給Shell可以用 export var1 var2 var3.... 範例: $DIR=/usr/proj ; export DIR $make jgref (可以在描述檔裡面用DIR這個shell變數,就好像把DIR當做巨集名稱一樣) SRC = ${DIR}/src jgref : cd ${DIR};

  10. Shell以及Makefile變數 Introduction to Make 10 想要把shell變數的值傳給Makefile VAR := $(shell shell_command)

  11. 目標 Introduction to Make 11

  12. 目標 Introduction to Make 12 .PHONY:在這個後面的target無條件執行。 例如 .Phony : clean clean: rm *.o .SUFFIXS: make有一些內定方法編譯特別副檔名,這些副檔名規則的副檔名 (名單)list,是在SUFFIXS這個變數裡, 可能有.c .o .cpp 等等 .SUFFIXS: (用SUFFIXS清掉內定副檔名list。 ) 例如: .SUFFIXS: .sgml .hack (加上.sgml .hack到內定list。)

  13. 目標 Introduction to Make 13 .SILENT:這裡面的target執行時 命令(command)將不會印出來 .EXPORT_ALL_VARIABLES:把所有變數告訴後來sub shell的子程序

  14. 命令 Introduction to Make 14

  15. 命令 Introduction to Make 15 command 前面一定要是個TAB鍵。不可以用空白鍵。 每一行的命令其實都是喚起一個sub shell來執行命令,做完了, 這個sub shell就沒有了。 要把錯誤掠過不看在命令前加個- 要不印出命令在螢幕上加@ 。 喚起的sub shell要用什麼shell,是定義在SHELL這個變數裡。

  16. 範例 Introduction to Make 16

  17. 範例一 Introduction to Make 17 假設執行檔包含了四個原始碼檔案,分別是 main.c a.c b.c c.c ,如何讓這個程式可以執行? 1.不用makefile在command line下以下指令 製作目標檔 gcc -c main.c gcc -c a.c gcc -c b.c gcc -c c.c 製作執行檔 gcc -o main main.o a.o b.o c.o -lm -L/usr/lib -L/lib 執行 ./main

  18. 範例一 Introduction to Make 18 2.使用makefile 建立makefile檔,內容如下 cc -c -o main.o main.c cc -c -o a.o a.c cc -c -o b.o b.c cc -c -o c.o c.c gcc -o main main.o a.o b.o c.o -lm

  19. 範例二 Introduction to Make 19 用makefile寫一個Quik sort,有10個變數 以下是Makefile內容: TRUE = 1 gt = $(shell if [ $1 -gt $2 ] ; then echo $(TRUE); fi) lt = $(shell if [ $1 -lt $2 ] ; then echo $(TRUE); fi) le = $(shell if [ $1 -le $2 ] ; then echo $(TRUE); fi)

  20. 範例二 Introduction to Make 20 qsort = \ $(if $(call le,$(words $1),1),$1, \ $(call qsort, \ $(foreach i,$1, \ $(if $(call gt,$(firstword $1),$i), $i,))) \ $(firstword $1) \ $(call qsort, \ $(foreach i,$1, \ $(if $(call lt,$(firstword $1),$i), $i,)))) data = $(shell od -vAn -N10 -w1 -tu1 < /dev/urandom) all: @echo $(call qsort, $(data))

  21. Reference • Tutorial Makefile (http://www.opussoftware.com/tutorial/TutMakefile.htm) • Peter’s Makefile Tutorial ( http://www.dirac.org/p/teaching/tutorials/makefiles/makefiles.html) • 用Makefile實現Quiksort ( http://blog.linux.org.tw/~jserv/archives/002035.html) Introduction to Make

More Related