Makefile
Makefile 是用於自動化建置流程的工具,常用於 C/C++ 專案。
CLI
version
make --versionGlossary
@ 符號
@ 可避免印出 command:
target:
@echo "This command will be echoed"
@echo "This command will also be echoed"
echo "This command will be displayed"
Output:
This command will be echoed
This command will also be echoed
echo "This command will be displayed"
This command will be displayedExample
C/C++ 專案
# specify the compiler
CC=gcc
# specify options for the compiler
CFLAGS=-c -Wall
all: hello
hello: main.o hello.o
$(CC) main.o hello.o -o hello
main.o: main.cpp
$(CC) $(CFLAGS) main.cpp
hello.o: hello.cpp
$(CC) $(CFLAGS) hello.cpp
clean:
rm -rf *o hello
另一個範例
output: main.o message.o
g++ main.o message.o -o output
main.o: main.cpp
g++ -c main.cpp
message.o: message.cpp message.h
g++ -c message.cpp
clean:
rm *.o output
基本結構
target: dependencies
command
| 元素 | 說明 |
|---|---|
target | 要建置的目標 |
dependencies | 建置目標所需的依賴 |
command | 建置目標的命令(必須以 Tab 開頭) |
常用變數
| 變數 | 說明 |
|---|---|
$@ | 目標名稱 |
$< | 第一個依賴 |
$^ | 所有依賴 |
$(CC) | 編譯器 |
$(CFLAGS) | 編譯選項 |