讀書心得 Managing Projects with GNU Make (1-2) Targets and Prerequisites

本文出自 Managing Projects with GNU Make 3rd-Robert Mecklenburg(2005)

Chp1. How to Write a Simple Makefile


回顧一下上一篇 makefile 的內容:

hello: hello.c
        gcc hello.c -o hello
target: prereq1 prereq2...
        commands

對照一下會發現 target-> hello, prereq1-> hello.c, commands-> gcc hello.c -o hello

target: 要 build 的目標
prereq: 需要的 file
command: build target 對應的動作


大家試著猜猜看這個 makefile build 的過程吧:

count_words: count_words.o lexer.o -lfl
        gcc count_words.o lexer.o -lfl -ocount_words

count_words.o: count_words.c
        gcc -c count_words.c

lexer.o: lexer.c
        gcc -c lexer.c

lexer.c: lexer.l
        flex -t lexer.l > lexer.c

make 的過程如下:

$ make
gcc -c count_words.c
flex -t lexer.l > lexer.c
gcc -c lexer.c
gcc count_words.o lexer.o -lfl -ocount_words

開始體會到 makefile 的強大了嗎? 在 build 的過程中他會自動去判斷各個 target 的相依性,方便我們 build code


順帶一提 target 可以不唯一:

target1 target2 target3: prerequisite1 prerequisite2
        command1
        command2
        command3

留言

熱門文章