讀書心得 Managing Projects with GNU Make (2-2) Automatic Variables

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

Chp2. Rules


Automatic Variables:

為了方便編寫 makefile,make 定義了一些 Automatic Variables (自動變數)

Automatic VariableDescription
$@代表 target 的字串
$%The filename element of an archive member specification.
$<第一個 prerequisite 字串
$?The names of all prerequisites that are newer than the target, separated by spaces.
$^所有 prerequisites 用空白分隔並去除掉重複的 prerequisite
$+Similar to $^, this is the names of all the prerequisites separated by spaces, except that $+ includes duplicates. This variable was created for specific situations such as arguments to linkers where duplicate values have meaning.
$*The stem of the target filename. A stem is typically a filename without its suffix.

來看個例子吧:

count_words: count_words.o counter.o lexer.o -lfl
        gcc $^ -o $@  # $^: 所有 prerequisites (count_words.c)
                       # $@: 代表 target 的字串 (count_words)
        # 查看 $^ 和 $@ 的方法
        # @echo $^
        # @echo $@
count_words.o: count_words.c
        gcc -c $<    # $<: 第一個 prerequisite (count_words.c)
counter.o: counter.c
        gcc -c $<
lexer.o: lexer.c
        gcc -c $<
lexer.c: lexer.l
        flex -t $< > $@

留言

熱門文章