makeコマンドのオプションの一部のまとめになり、コマンド例とその実行結果を紹介しています。また、ここでのオプションはGNU makeでのオプションになります。
makeについては以下の記事で紹介しています。

makeコマンドのオプションについて その2もあります。

目次
- 1 実行するmakefileの指定(-fオプション)
- 2 タイムスタンプに関係なくレシピを実行(-Bオプション)
- 3 指定したファイルのレシピを実行しない(-oオプション)
- 4 makefileを読み込む前にディレクトリを移動(-Cオプション)
- 5 makefileの実行前にディレクトリ情報を表示(-wオプション)
- 6 エラーを無視(-iオプション)
- 7 エラーが起きてもできる限り実行(-kオプション)
- 8 変数を環境変数でオーバライド(-eオプション)
- 9 文字列をmakefileの構文として実行(--evalオプション)
- 10 シンボリックリンクファイルのタイムスタンプも参照(-Lオプション)
- 11 デフォルトの暗黙的なルールをクリア(-rオプション)
- 12 デフォルトの暗黙的なルールとその変数をクリア(-Rオプション)
- 13 ファイルに対して touchのみを実行(-tオプション)
実行するmakefileの指定
(-fオプション)
-fオプションは実行するmakefileを指定できます。デフォルトでmakeを実行する以外のファイルを指定する場合に利用できます。
file.mk
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 5 6 7 8 |
$ ls file.mk $ $ make -ffile.mk touch file1.txt cp file1.txt target.txt $ ls file.mk file1.txt target.txt |
変数$@はターゲットファイルを表すmakeの変数になります。
また、変数$<は一番初めに記述されている依存ファイルを表すmakeの変数になります。
タイムスタンプに関係なくレシピを実行
(-Bオプション)
makeはターゲットファイルと依存ファイルのタイムスタンプの関係で、ターゲットファイルが最新の状態の場合、そのターゲットファイルを生成するレシピを実行しません。
-Bオプションはタイムスタンプに関係なく、レシピを実行することができます。
makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 |
$ ls --full-time -go 合計 4 -rw-rw-r-- 1 0 2018-04-29 23:05:26.200017999 +0900 file1.txt -rw-rw-r-- 1 56 2018-04-29 14:57:35.471125000 +0900 makefile -rw-rw-r-- 1 0 2018-04-29 23:05:26.200017999 +0900 target.txt $ $ make make: 'target.txt' is up to date. $ $ make -B touch file1.txt cp file1.txt target.txt |
指定したファイルのレシピを実行しない
(-oオプション)
-oオプションは指定したファイルのタイムスタンプが依存ファイルより古い場合等でもmakeのレシピを実行しません。
makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
$ ls makefile $ $ make -o target.txt make: 'target.txt' is up to date. $ $ make -o file1.txt cp file1.txt target.txt cp: 'file1.txt' を stat できません: No such file or directory makefile:2: recipe for target 'target.txt' failed make: *** [target.txt] Error 1 $ $ make touch file1.txt cp file1.txt target.txt |
makefileを読み込む前にディレクトリを移動
(-Cオプション)
-Cオプションはmakefileを読み込む前にディレクトリを移動します。このオプションは再帰的にmakeを実行するときに便利なオプションになります。
makefile
1 2 3 4 |
.PHONY: all all: make -C dir |
dir/makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 5 6 7 8 |
$ ls dir makefile $ make make -C dir make[1]: Entering directory '/home/ubuntu/test_makeoptions/dir' touch file1.txt cp file1.txt target.txt make[1]: Leaving directory '/home/ubuntu/test_makeoptions/dir' |
makefileの実行前にディレクトリ情報を表示
(-wオプション)
-wオプションはmakefileの実行前や実行後にディレクトリ情報を表示します。
makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 5 |
$ make -w make: Entering directory '/home/ubuntu/test_makeoptions' touch file1.txt cp file1.txt target.txt make: Leaving directory '/home/ubuntu/test_makeoptions' |
また、--no-print-directoryオプションを利用するとディレクトリ情報の表示を抑制することができます。
makefile
1 2 3 4 |
.PHONY: all all: make -C dir |
dir/makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ file1.txt : touch $@ |
コマンド例と実行結果
1 2 3 4 |
$ make make -C dir --no-print-directory touch file1.txt cp file1.txt target.txt |
エラーを無視
(-iオプション)
makeで実行しているコマンドでエラーが起きた場合、makeの実行が中断されます。
-iオプションを用いると、レシピ内でエラーが起きても、レシピ内のコマンドが続けて実行され続けます。
makefile
1 2 3 4 5 |
target.txt : file1.txt cp $< $@ echo "copy file ..." file1.txt : |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ make -i cp file1.txt target.txt cp: 'file1.txt' を stat できません: No such file or directory makefile:2: recipe for target 'target.txt' failed make: [target.txt] Error 1 (ignored) echo "copy file ..." copy file ... $ $ make cp file1.txt target.txt cp: 'file1.txt' を stat できません: No such file or directory makefile:2: recipe for target 'target.txt' failed make: *** [target.txt] Error 1 |
また、makefileでコマンドの文頭に'-'をつけると、そのコマンドの部分でエラーが起きても続けて実行できます。
makefile
1 2 3 4 5 |
target.txt : file1.txt -cp $< $@ echo "copy file ..." file1.txt : |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 |
$ ls makefile $ $ make cp file1.txt target.txt cp: 'file1.txt' を stat できません: No such file or directory makefile:2: recipe for target 'target.txt' failed make: [target.txt] Error 1 (ignored) echo "copy file ..." copy file ... |
エラーが起きてもできる限り実行
(-kオプション)
-kオプションは、エラーが起きてもできる限り実行処理を行います。
例えば、あるターゲットファイルに対して、複数の依存ファイルが存在する時にある依存ファイルのレシピでエラーが起きた場合、そのレシピの処理は中断されますが、別の依存ファイルの処理はそのまま行うことができます。
makefile
1 2 3 4 5 6 7 8 9 |
target.txt : file1.txt file2.txt echo "$@" file1.txt : false echo "$@" file2.txt : echo "$@" |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 |
$ make -k false makefile:5: recipe for target 'file1.txt' failed make: *** [file1.txt] Error 1 echo "file2.txt" file2.txt make: Target 'target.txt' not remade because of errors. $ $ make false makefile:5: recipe for target 'file1.txt' failed make: *** [file1.txt] Error 1 |
また、再帰的にmakeを行う場合、-Sオプションを用いると-kオプションをキャンセルすることができます。
makefile
1 2 3 4 5 6 7 8 9 10 |
.PHONY: all process1 process2 all : process1 process2 process1 : @false process2 : @echo "process2 ..." make -S -C dir |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$ make -k makefile:6: recipe for target 'process1' failed make: *** [process1] Error 1 process2 ... make -S -C dir make[1]: Entering directory '/home/ubuntu/test_makeoptions/dir' false makefile:5: recipe for target 'file1.txt' failed make[1]: *** [file1.txt] Error 1 make[1]: Leaving directory '/home/ubuntu/test_makeoptions/dir' makefile:9: recipe for target 'process2' failed make: *** [process2] Error 2 make: Target 'all' not remade because of errors. $ $ make makefile:6: recipe for target 'process1' failed make: *** [process1] Error 1 |
コマンドの文頭に'@'をつけるとmakeでのコマンドのエコー表示を抑制することができます。
変数を環境変数でオーバライド
(-eオプション)
-eオプションを利用するとmakeの変数より環境変数を優先しオーバライドします。
makefile
1 2 3 4 |
TARGET=target.txt $(TARGET) : @echo '$@' |
コマンド例と実行結果
1 2 3 4 5 6 |
$ TARGET='test.txt' make -e test.txt $ TARGET='test.txt' make target.txt $ make target.txt |
文字列をmakefileの構文として実行
(--evalオプション)
--evalオプションを用いるとmakefileを読み込む前に文字列をmakefileの構文として読み込むことができます。また、--evalオプションはmakeでのeval関数のコマンドライン版になります。
makefile
1 2 3 |
.PHONY: all all : @echo 'all process ...' |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 |
$ make --eval='target.txt:file1.txt;cp $< $@' cp file1.txt target.txt $ $ rm target.txt $ make --eval='target.txt:file1.txt;cp $< $@' all all process ... $ make --eval='target.txt:file1.txt;cp $< $@' target.txt all cp file1.txt target.txt all process ... |
--evalオプションはmakefileを読み込む前に文字列を読み込むため、文字列によってはmakeのデフォルトゴールが変更されます。
シンボリックリンクファイルのタイムスタンプも参照
(-Lオプション)
通常、シンボリックリンクファイルを参照している場合、リンク先のタイムスタンプが参照されます。
-Lオプションを用いると、シンボリックリンクファイルのタイムスタンプも参照され、両方のファイルのタイムスタンプから新しい方のタイムスタンプをファイルのタイムスタンプとして扱います。
makefile
1 2 |
target.txt : file_sym.txt touch $@ |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
$ make make: 'target.txt' is up to date. $ ls file.txt file_sym.txt makefile target.txt $ $ touch file.txt $ make -L touch target.txt $ touch -h file_sym.txt $ make -L touch target.txt $ $ touch file.txt $ make touch target.txt $ touch -h file_sym.txt $ make make: 'target.txt' is up to date. |
デフォルトの暗黙的なルールをクリア
(-rオプション)
-rオプションはサフィックスルールのような暗黙的なルールの定義をクリアします。
デフォルトでどのようなサフィックスルールが存在するかはmakefileが存在しない状態で、makeのデータベースを確認する-pオプションが利用できます。
以下のコマンドは、envコマンドを用いて環境変数を取り除き、grepコマンドでコメント行と空行を取り除いてmakeのデータベースを確認します。
コマンド例
1 |
env -i make -rqp |grep -v -e '^#' -e'^ |
実行結果例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
make: *** No targets specified and no makefile found. Stop. <D = $(patsubst %/,%,$(dir $<)) ?F = $(notdir $?) .SHELLFLAGS := -c CWEAVE = cweave ?D = $(patsubst %/,%,$(dir $?)) @D = $(patsubst %/,%,$(dir $@)) @F = $(notdir $@) CURDIR := /home/ubuntu/test_makeoptions SHELL := /bin/sh RM = rm -f CO = co PREPROCESS.F = $(FC) $(FFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -F LINK.m = $(OBJC) $(OBJCFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) LINK.o = $(CC) $(LDFLAGS) $(TARGET_ARCH) OUTPUT_OPTION = -o $@ COMPILE.cpp = $(COMPILE.cc) MAKEFILE_LIST := GNUMAKEFLAGS := LINK.p = $(PC) $(PFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) CC = cc CHECKOUT,v = +$(if $(wildcard $@),,$(CO) $(COFLAGS) $< $@) CPP = $(CC) -E LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) MAKE_HOST := x86_64-pc-linux-gnu LD = ld TEXI2DVI = texi2dvi YACC = yacc COMPILE.mod = $(M2C) $(M2FLAGS) $(MODFLAGS) $(TARGET_ARCH) ARFLAGS = rv LINK.r = $(FC) $(FFLAGS) $(RFLAGS) $(LDFLAGS) $(TARGET_ARCH) LINT = lint COMPILE.f = $(FC) $(FFLAGS) $(TARGET_ARCH) -c LINT.c = $(LINT) $(LINTFLAGS) $(CPPFLAGS) $(TARGET_ARCH) YACC.m = $(YACC) $(YFLAGS) YACC.y = $(YACC) $(YFLAGS) AR = ar .FEATURES := target-specific order-only second-expansion else-if shortest-stem undefine oneshell archives jobserver output-sync check-symlink load TANGLE = tangle GET = get %F = $(notdir $%) COMPILE.F = $(FC) $(FFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c CTANGLE = ctangle .LIBPATTERNS = lib%.so lib%.a LINK.C = $(LINK.cc) LINK.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_MACH) PREPROCESS.r = $(FC) $(FFLAGS) $(RFLAGS) $(TARGET_ARCH) -F LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) LINK.s = $(CC) $(ASFLAGS) $(LDFLAGS) $(TARGET_MACH) ^D = $(patsubst %/,%,$(dir $^)) MAKELEVEL := 0 COMPILE.m = $(OBJC) $(OBJCFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c MAKE = $(MAKE_COMMAND) AS = as PREPROCESS.S = $(CC) -E $(CPPFLAGS) COMPILE.p = $(PC) $(PFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c MAKE_VERSION := 4.1 FC = f77 .DEFAULT_GOAL := %D = $(patsubst %/,%,$(dir $%)) WEAVE = weave MAKE_COMMAND := make LINK.cpp = $(LINK.cc) F77 = $(FC) COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c .VARIABLES := PC = pc *F = $(notdir $*) COMPILE.def = $(M2C) $(M2FLAGS) $(DEFFLAGS) $(TARGET_ARCH) LEX = lex MAKEFLAGS = pqr MFLAGS = -pqr *D = $(patsubst %/,%,$(dir $*)) LEX.l = $(LEX) $(LFLAGS) -t LEX.m = $(LEX) $(LFLAGS) -t +D = $(patsubst %/,%,$(dir $+)) COMPILE.r = $(FC) $(FFLAGS) $(RFLAGS) $(TARGET_ARCH) -c +F = $(notdir $+) M2C = m2c MAKEFILES := <F = $(notdir $<) CXX = g++ COFLAGS = COMPILE.C = $(COMPILE.cc) ^F = $(notdir $^) COMPILE.S = $(CC) $(ASFLAGS) $(CPPFLAGS) $(TARGET_MACH) -c LINK.F = $(FC) $(FFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) SUFFIXES := COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c COMPILE.s = $(AS) $(ASFLAGS) $(TARGET_MACH) .INCLUDE_DIRS = /usr/include /usr/local/include /usr/include .RECIPEPREFIX := MAKEINFO = makeinfo MAKE_TERMERR := /dev/pts/17 OBJC = cc TEX = tex F77FLAGS = $(FFLAGS) LINK.f = $(FC) $(FFLAGS) $(LDFLAGS) $(TARGET_ARCH) Makefile: makefile: .DEFAULT: GNUmakefile: |
デフォルトの暗黙的なルールとその変数をクリア
(-Rオプション)
-Rオプションはデフォルトの暗黙的なルールの変数をクリアします。このオプションは-rオプションも自動的にオンになるため、結果として、デフォルトの暗黙的なルールとその変数をクリアすることになります。
コマンド例
1 |
env -i make -Rqp |grep -v -e '^#' -e'^ |
実行結果例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
make: *** No targets specified and no makefile found. Stop. <D = $(patsubst %/,%,$(dir $<)) ?F = $(notdir $?) .SHELLFLAGS := -c ?D = $(patsubst %/,%,$(dir $?)) @D = $(patsubst %/,%,$(dir $@)) @F = $(notdir $@) CURDIR := /home/ubuntu/test_makeoptions SHELL := /bin/sh MAKEFILE_LIST := GNUMAKEFLAGS := MAKE_HOST := x86_64-pc-linux-gnu .FEATURES := target-specific order-only second-expansion else-if shortest-stem undefine oneshell archives jobserver output-sync check-symlink load %F = $(notdir $%) ^D = $(patsubst %/,%,$(dir $^)) MAKELEVEL := 0 MAKE = $(MAKE_COMMAND) MAKE_VERSION := 4.1 .DEFAULT_GOAL := %D = $(patsubst %/,%,$(dir $%)) MAKE_COMMAND := make .VARIABLES := *F = $(notdir $*) MAKEFLAGS = pqrR MFLAGS = -pqrR *D = $(patsubst %/,%,$(dir $*)) +D = $(patsubst %/,%,$(dir $+)) +F = $(notdir $+) MAKEFILES := <F = $(notdir $<) ^F = $(notdir $^) SUFFIXES := .INCLUDE_DIRS = /usr/include /usr/local/include /usr/include .RECIPEPREFIX := MAKE_TERMERR := /dev/pts/17 Makefile: makefile: .DEFAULT: GNUmakefile: |
ファイルに対して touchのみを実行
(-tオプション)
-tオプションを用いるとレシピを実行する代わりにtouchコマンドを実行します。このオプションを用いるとレシピを実行しないでmakeで生成するファイルのタイムスタンプを変更できます。
makefile
1 2 3 4 5 |
target.txt : file.txt echo "target.txt process ..." file.txt : echo "file.txt process ..." |
コマンド例と実行結果
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ ls makefile $ make -t touch file.txt touch target.txt $ make make: 'target.txt' is up to date. $ $ touch file.txt $ make -t touch target.txt $ make make: 'target.txt' is up to date. |