初識makefile結構

2021-09-11 11:36:51 字數 1107 閱讀 1596

#依賴的定義:

targets:prerequisites;command1

'\t'

command2

複製**

all:test

echo

"make all"

test:

echo

"make test"

複製**

目標all依賴於test,當執行all的時候,執行過程為:先執行test依賴與命令,然後執行all內部的命令

makefile中可以在命令前加上@符,作用為命令無回顯。

hello.out:main.o func.o

gcc -o hello.out main.o func.o

mail.o:main.c

gcc -o main.o -c main.c

func.o:func.c

gcc -o func.o -c func.c

複製**

示例中的依賴關係如下:

工程開發中可以將最終可執行檔名和all同時作為makefile中第一條規則的目標。

hello.out all:main.o func.o

gcc -o hello.out main.o func.o

複製**

從編譯執行結果可知:

--第一次執行make命令的時候,編譯器會根據依賴關係呼叫對應的命令,並同時生成目標檔案func.o和main.o,

--第二次或者第三次執行make命令的時候,雖然目標檔案func.o和main.o並沒有做任何更改,但是因為編譯器沒有生成hello.out,故編譯器每次都會執行all命令去生成目標檔案hello.out。

從編譯執行結果可知:

當使用最終可執行檔名hello.out和all同時作為makefile中第一條規則的目標的時候,

--第一次執行make命令的時候,編譯器會生成同時目標檔案func.o、main.o、hello.out;

--第二次執行make命令的時候,由於目標檔案hello.out已經存在,故編譯器不會再執行all目標之後的命令。

#小結

makefile 結構初識

targets prerequisites command1 t command2 all test echo make all test echo make test 目標all依賴於test,當執行all的時候,執行過程為 先執行test依賴與命令,然後執行all內部的命令 makefile中可...

02 初識 makefile 的結構

targets 通常是需要生成的目標檔名 make 所需執行的命令名稱 prerequisities 當前目標所依賴的其它目標或檔案 command 完成目標所需要執行的命令 規則中的注意事項 依賴規則 依賴示例 all test echo make all test echo make test ...

第二課 初識makefile的結構

makefile的意義 乙個最基本的依賴的規則如下 target prerequisites command1 t command2 makefile中的元素含義 prerequisities command 規則中的注意事項 續行符 乙個makefile的依賴示例 all test echo ma...