- Makefile 教程
- Makefile - 主頁
- Makefile - 為什麼使用 Makefile?
- Makefile - 宏
- Makefile - 依賴項
- Makefile - 規則
- Makefile - 字尾規則
- Makefile - 指令
- Makefile - 重新編譯
- Makefile - 其他功能
- Makefile - 示例
- Makefile 快速指南
- Makefile - 快速指南
- Makefile - 有用資源
- Makefile - 討論
在 Makefile 中定義自定義字尾規則
Make 可以使用 cc -c 自動建立 .o 檔案,對應於 .c 檔案。這些規則已內建到 make 中,你可以利用此優勢來縮短 Makefile。如果你僅在 Makefile 中的依賴關係行中指出了 Makefile 所依賴的 .h 檔案,make 會知道它也需要相應的 .c 檔案。你無需輸入編譯器命令。
這樣會進一步精簡 Makefile,如下所示:-
OBJECTS = main.o hello.o factorial.o hello: $(OBJECTS) cc $(OBJECTS) -o hello hellp.o: functions.h main.o: functions.h factorial.o: functions.h
make 使用名為 .SUFFIXES 的特殊目標,讓你可以定義自己的字尾。例如,參見下面給出的依賴項行:-
.SUFFIXES: .foo .bar
它告知 make 你將使用這些特殊字尾來制定自己的規則。
與 make 已知從 .c 檔案生成 .o 檔案的方式類似,你可以按照以下方式定義規則:-
.foo.bar: tr '[A-Z][a-z]' '[N-Z][A-M][n-z][a-m]' < $< > $@ .c.o: $(CC) $(CFLAGS) -c $<
第一個規則讓你可以從 .foo 檔案建立 .bar 檔案。它基本上會擾亂檔案。第二個規則是 make 用來從 .c 檔案建立 .o 檔案的預設規則。
廣告