Makefile

Makefile はビルドプロセスを自動化(じどうか) するためのツールで、C/C++ プロジェクトでよく使用(しよう) されます。

CLI

version

make --version

Glossary

@ 記号

@ はコマンドの出力(しゅつりょく)抑制(よくせい) できます:

target:
    @echo "This command will be echoed"
    @echo "This command will also be echoed"
    echo "This command will be displayed"

出力(しゅつりょく)

This command will be echoed
This command will also be echoed
echo "This command will be displayed"
This command will be displayed

Example

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)コンパイルオプション