Makefile

makefile とは、ターゲットのビルドを記述し、かつソース・レベルの依存関係やビルド順序 の依存関係などの情報が含まれる、make コマンドで参照されるテキスト・ファイルのことです。

CDT はユーザーの代わりに makefile を生成することができます。このようなプロジェクトは、 管理 Make プロジェクトと呼ばれています。 スタンダード Make プロジェクトと呼ばれるプロジェクトでは、ユーザー独自の makefile を定義できます。

サンプル Makefile

# A sample Makefile
# This Makefile demonstrates and explains
# Make Macros, Macro Expansions,
# Rules, Targets, Dependencies, Commands, Goals
# Artificial Targets, Pattern Rule, Dependency Rule.

# Comments start with a # and go to the end of the line.

# Here is a simple Make Macro.
LINK_TARGET = test_me.exe

# Here is a Make Macro that uses the backslash to extend to multiple lines.
# This allows quick modification of more object files.
OBJS =  ¥
 Test1.o ¥
 Test2.o ¥
 Main.o

# Here is a Make Macro defined by two Macro Expansions.
# A Macro Expansion may be treated as a textual replacement of the Make Macro.
# Macro Expansions are introduced with $ and enclosed in (parentheses).
REBUILDABLES = $(OBJS) $(LINK_TARGET)

# Make Macros do not need to be defined before their Macro Expansions,
# but they normally should be defined before they appear in any Rules.
# Consequently Make Macros often appear first in a Makefile.

# Here is a simple Rule (used for "cleaning" your build environment).
# It has a Target named "clean" (left of the colon ":" on the first line),
# no Dependencies (right of the colon),
# and two Commands (indented by tabs on the lines that follow).
# The space before the colon is not required but added here for clarity.
clean :  rm -f $(REBUILDABLES)
 echo Clean done

# There are two standard Targets your Makefile should probably have:
# "all" and "clean", because they are often command-line Goals.
# Also, these are both typically Artificial Targets, because they don't typically
# correspond to real files named "all" or "clean".

# The rule for "all" is used to incrementally build your system.
# It does this by expressing a dependency on the results of that system,
# which in turn have their own rules and dependencies.
all : $(LINK_TARGET)
 echo All done

# There is no required order to the list of rules as they appear in the Makefile.
# Make will build its own dependency tree and only execute each rule only once
# its dependencies' rules have been executed successfully.

# Here is a Rule that uses some built-in Make Macros in its command:
# $@ expands to the rule's target, in this case "test_me.exe".
# $^ expands to the rule's dependencies, in this case the three files
# main.o, test1.o, and  test2.o.
$(LINK_TARGET) : $(OBJS)
 g++ -g -o $@ $^

# Here is a Pattern Rule, often used for compile-line.
# It says how to create a file with a .o suffix, given a file with a .cpp suffix.
# The rule's command uses some built-in Make Macros:
# $@ for the pattern-matched target
# $lt; for the pattern-matched dependency
%.o : %.cpp
 g++ -g -o $@ -c $<

# These are Dependency Rules, which are rules without any command.
# Dependency Rules indicate that if any file to the right of the colon changes,
# the target to the left of the colon should be considered out-of-date.
# The commands for making an out-of-date target up-to-date may be found elsewhere
# (in this case, by the Pattern Rule above).
# Dependency Rules are often used to capture header file dependencies.
Main.o : Main.h Test1.h Test2.h
Test1.o : Test1.h Test2.h
Test2.o : Test2.h

# Alternatively to manually capturing dependencies, several automated
# dependency generators exist.  Here is one possibility (commented out)...
# %.dep : %.cpp
#        g++ -M $(FLAGS) $< > $@
# include $(OBJS:.o=.dep)

よくある質問:

「コンソール」ビューは、ビルドをデバッグする際に非常に有用です。

Q1. 「コンソール」ビューに「ビルダー起動中のエラー」と表示されます。 これは何を意味していますか?

Error launching builder (make -k clean all )
(Exec error:Launching failed)

最も考えられる原因は、ビルド・コマンド (デフォルトは "make") がパス上にないということです。 このコマンドをパスに書き込んでから、Eclipse を再始動してください。
また、ビルド・コマンドを、パス上にあるものに変更することもできます。 MinGW ツールを使用してコンパイルしている場合は、ビルド・コマンドを "mingw32-make" で 置き換える必要があります。

Q2. 「コンソール」ビューに「No rule to make target 'X'」 と表示されます。

make -k clean all
make: *** No rule to make target 'clean'.
make: *** No rule to make target 'all'.

デフォルトでは、make プログラムは、一般に "Makefile" または "makefile" と呼ばれるファイルを検索します。 作業ディレクトリーでこのようなファイルを検出できない場合や、あったとしてもそのファイルが空であったり、 コマンド行ゴールのルール (この場合、"clean" および "all") が含まれていなかったりする場合には、 通常は失敗して、上記のようなエラー・メッセージが表示されます。

有効な Makefile が既に存在するならば、ビルドの作業ディレクトリーを変更しなければならない可能性があります。 ビルド・コマンドのデフォルトの作業ディレクトリーは、プロジェクトのルート・ディレクトリーです。 これは、Make プロジェクト・プロパティーで別のビルド・ディレクトリーを指定することで変更できます。 あるいは、Makefile に別の名前が付いている場合 (例: buildFile.mk)、 デフォルトのビルド・コマンドを make -f buildFile.mk に設定することで、 その名前を指定できます。

有効な Makefile がない場合は、ルート・ディレクトリーに Makefile という名前の新規ファイルを作成してください。 その後、サンプル Makefile (上述) の内容を追加し、それを適宜変更することができます。

Q3. 「コンソール」ビューに「missing separator」と表示されます。

make -k clean all
makefile:12: *** missing separator.  Stop.

Makefile の標準構文は、ビルド・ルールの全行の前にタブ文字を使用する必要があるこ とを指示しています。 このタブ文字は、誤ってスペースに置き換えられることがよくあります、 タブ文字とスペースの結果はともに、空白のインデントとなるため、これは見過ごされやすい問題です。 この提供されているサンプルでは、エラー・メッセージからファイル makefile の 12 行目が特定できます。 この問題を修正するには、その行の先頭にタブを挿入してください。

Q4. 「コンソール」ビューに、「Target 'all' not remade because of errors」と表示されます。

make -k clean all
make: *** [clean] Error 255
rm -f Test1.o Test2.o Main.o test_me.exe
g++ -g -o Test1.o -c Test1.cpp
make: *** [Test1.o] Error 255
make: *** [Test2.o] Error 255
make: *** [Main.o] Error 255
g++ -g -o Test2.o -c Test2.cpp
g++ -g -o Main.o -c Main.cpp
make: Target 'all' not remade because of errors.

ここで考えられる原因は、g++ がパス上にないことです。

エラー 255 は、コマンド・シェルが特定ルールのコマンドを検出できない結果として、 make によって生成されます。
標準エラー・ストリーム (Error 255 を示す行) および標準出力ストリーム (他のすべての行) からのメッセージが「コンソール」ビューでマージされます。

Q5. -k フラグとは何ですか?

-k フラグは、make に対し、1 つのルールが失敗した場合でも、他の独立ルールの作成を継続 するように指示します。 これは、大きなプロジェクトのビルドの場合に有用です。

-k フラグは、「プロジェクト・プロパティー」>「C/C++ Make プロジェクト」> 「Make ビルダー」>「最初のビルド・エラーで停止 (Stop on first build error)」をオン にすると除去できます。

Q6. 「コンソール」ビューの表示が以下のようになっています。

mingw32-make clean all
process_begin: CreateProcess((null), rm -f Test1.o Test2.o Main.o test_me.exe, ...) failed.
make (e=2): The system cannot find the file specified.

mingw32-make: *** [clean] Error 2
rm -f Test1.o Test2.o Main.o test_me.exe

これは、mingw32-make がユーティリティー「rm」を検出できなかったことを意味します。残念ながら、MinGW には「rm」が用意されていません。 これを訂正するには、Makefile の clean ルールを以下のもので置き換えます。

clean : 	-del $(REBUILDABLES)
	echo Clean done

先行する負符号 (-) によって、del コマンドが失敗を戻す場合であっても、 clean ルールを成功させるように make に指示します。 これは、削除するものとして指定されたファイルがまだ (あるいは既に) 存在しない場合には del コマンドが失敗するため、受け入れ可能になります。

IBM 著作権文