1.@關閉命令的回顯linux
2.make帶入參數「-n」或「--just-print」,只是顯示命令,但不會執行命令,這個功能方便調試 Makefile。數據庫
3.make 參數「-s」或「--slient」全面禁止命令的顯示。bash
make 逐條執行其後的命令。 若是打算上一條命令結果應用到下一條命令,須要把要執行的命令寫在同一行使用分號;隔開。less
test:
cd ./path1
pwd
複製代碼
運行結果:ide
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1
pwd
/home/workspace/my_workspace/study/makefile
複製代碼
test:
cd ./path1;pwd
複製代碼
運行結果:ui
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1;pwd
/home/workspace/my_workspace/study/makefile/path1
複製代碼
makefile使用mkdir建立目錄時,不存在時makefile正常運行,可是目錄存在則沒法mkdir時出現報錯,這個並不該該影響到後面命令的執行。this
test:
@mkdir path1
@echo continue
複製代碼
運行結果:spa
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: *** [test] Error 1
複製代碼
命令執行錯誤直接終止。 解決辦法 (1)Makefile命令行前加一個減號- (2)全局的辦法,給 make 加上「-i」或是「--ignore-errors」參數。命令行
test:
-@mkdir path1
@echo continue
複製代碼
運行結果:debug
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
continue
複製代碼
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test --ignore-errors
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
continue
複製代碼
若是一個規則是以「.IGNORE」做爲目標的,那麼這個規則中的全部命令將會忽略錯誤。
.IGNORE: test
test:
@mkdir path1
@cat no_exist_file
@echo continue
複製代碼
運行結果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
mkdir: cannot create directory ‘path1’: File exists
make: [test] Error 1 (ignored)
cat: no_exist_file: No such file or directory
make: [test] Error 1 (ignored)
continue
複製代碼
根據功能和模塊將相關的文件放置在不一樣的文件夾中,每一個文件夾中單首創建一個Makefile來進行管理維護。最外層有個總控Makefile,能夠實現全編譯。
定義$(MAKE)宏變量的意思是,定義成一個變量便於make參數傳遞,利於維護。
subdir = ./path1
test:
cd $(subdir) && $(MAKE)
複製代碼
運行結果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
複製代碼
總控Makefile中的參數要傳遞給子目錄下的Makefile。 只需在總控Makefile的變量前添加export便可,相反若是特定變量不想傳遞下去則使用unexport。
總控Makefile
export subdir = ./path1
test:
cd $(subdir) && $(MAKE)
複製代碼
子目錄path1中的Makefile
hello.o: hello.c
gcc -c hello.c
echo $(subdir)
複製代碼
運行結果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c echo ./path1 ./path1 make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
複製代碼
若是不少變量要傳遞,只須要添加export關鍵字,後面不須要跟變量名。
export
subdir = ./path1
other = 666
test:
cd $(subdir) && $(MAKE) && rm *.o
複製代碼
運行結果:
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make test
cd ./path1 && make && rm *.o
make[1]: Entering directory `/home/workspace/my_workspace/study/makefile/path1' gcc -c hello.c ./path1 666 make[1]: Leaving directory `/home/workspace/my_workspace/study/makefile/path1'
複製代碼
注意: SHELL、MAKEFLAGE這兩個變量比較特殊,無論是否export它們都會傳遞到子目錄Makefile中。MAKEFLAGE仍是系統級別的環境變量。
總控Makefile定義的變量傳遞到下級Makefile中,若是下級Makefile有定義同名的變量。下級Makefile變量值不會被覆蓋。若是想要覆蓋,執行makefile的時傳遞參數-e -e, --environment-overrides Environment variables override makefiles. 覆蓋makefile環境變量。
make的參數還蠻多的
root@chenwr-pc:/home/workspace/my_workspace/study/makefile# make --help
Usage: make [options] [target] ...
Options:
-b, -m Ignored for compatibility.
(忽略的兼容性)
-B, --always-make Unconditionally make all targets.
(無條件完成全部目標)
-C DIRECTORY, --directory=DIRECTORY
Change to DIRECTORY before doing anything.
(在作任何事情以前切換到目錄)
-d Print lots of debugging information.
(打印大量調試信息)
--debug[=FLAGS] Print various types of debugging information.
(打印各類類型的調試信息)
-e, --environment-overrides
Environment variables override makefiles.
(環境變量覆蓋makefile)
-f FILE, --file=FILE, --makefile=FILE
Read FILE as a makefile.
(將文件讀取爲makefile)
-h, --help Print this message and exit.
(打印該信息並退出)
-i, --ignore-errors Ignore errors from commands.
(忽略來自命令的錯誤)
-I DIRECTORY, --include-dir=DIRECTORY
Search DIRECTORY for included makefiles.
(搜索包含makefile的目錄)
-j [N], --jobs[=N] Allow N jobs at once; infinite jobs with no arg.
(一次容許N個做業;沒有參數的無限做業)
-k, --keep-going Keep going when some targets can't be made. (當一些目標沒法達成時,繼續執行) -l [N], --load-average[=N], --max-load[=N] Don't start multiple jobs unless load is below N.
(除非負載小於N,不然不要啓動多個做業)
-L, --check-symlink-times Use the latest mtime between symlinks and target.
(使用符號連接和目標之間的最新時間)
-n, --just-print, --dry-run, --recon
Don't actually run any commands; just print them. (不要實際運行任何命令;只是打印) -o FILE, --old-file=FILE, --assume-old=FILE Consider FILE to be very old and don't remake it.
(考慮文件是很是舊的,不要從新建立)
-p, --print-data-base Print make's internal database. (打印make的內部數據庫) -q, --question Run no commands; exit status says if up to date. (運行任何命令;退出狀態表示是否最新) -r, --no-builtin-rules Disable the built-in implicit rules. (禁用內置的隱式規則) -R, --no-builtin-variables Disable the built-in variable settings. (禁用內置變量設置) -s, --silent, --quiet Don't echo commands.
(不要echo命令)
-S, --no-keep-going, --stop
Turns off -k.
-t, --touch Touch targets instead of remaking them.
(觸摸目標,而不是重作)
-v, --version Print the version number of make and exit.
(打印make和exit的版本號)
-w, --print-directory Print the current directory.
(打印當前目錄)
--no-print-directory Turn off -w, even if it was turned on implicitly.
(關閉-w,即便它是隱式打開的)
-W FILE, --what-if=FILE, --new-file=FILE, --assume-new=FILE
Consider FILE to be infinitely new.
(認爲文件是無限新的)
--warn-undefined-variables Warn when an undefined variable is referenced.
(引用未定義的變量時發出警告)
This program built for x86_64-pc-linux-gnu
Report bugs to <bug-make@gnu.org>
複製代碼
make 的參數「-k」或「--keep-going」,參數意思是,若是某規則中的命令出錯了,那麼就終目該規則的執行,但繼續執行其它規則。
可是 make 命令中的有幾個參數並不往下傳遞,它們是「-C」,「-f」,「-h」「-o」和「-W」。 當你使用「-C」參數來指定 make 下層 Makefile 時,「-w」會被自動打開的。若是參數中有「-s」(「--slient」)或是「--no-print-directory」,那麼,「-w」老是失效的。
makefile中的命令包寫法相似C語言中的define
define my_action
touch smile
endef
test:
$(my_action)
複製代碼
定義一個my_action 它的功能是建立個smile文件。調用方式也跟引用變量的方式是同樣的。make 在執行命令包時,命令包中的每一個命令會被依次獨立執行。