前回,プログラミング作業の効率化のためソースファイルを分割したが, かえってコンパイル作業が面倒なことになっていた. そこで今回は,makeコマンドを利用し,分割コンパイルを自動化しよう.
基本的な技については, とりあえず こちら (昨年度の授業内容)を参考に学習してください.
... .SUFFIXES: .c .o .c.o: cc -c $< -Wall # ファイル.c から ファイル.o を作る共通のコマンド file1.o: file1.c hdr1.h file2.o: file2.c hdr2.h ...
ついでに,ファイル間の依存関係の記述が面倒なので, 自動生成してしまおう:
$ cc -MM *.c file1.o: file1.c hdr1.h file2.o: file2.c hdr2.h ... $ cc -MM *.c >> Makefile
.PHONY: all ... all: プログラム1 プログラム2 ... プログラム1: オブジェクト11.o オブジェクト12.o ... cc ... -o プログラム1 プログラム2: オブジェクト21.o オブジェクト22.o ... cc ... -o プログラム2 ...
$ make all
なお,引数なしで make コマンドを実行すると, Makefile 内の最初のターゲットを生成しようとする. というわけで,all ターゲットについては, Makefile の先頭あたりに最初のエントリとして書いておくと良い. また,プログラムが1個だけの場合でも,all を書いておくと良い.
... .PHONY: distclean dist ... distclean: ... -rm ... dist: distclean ( cd ..; tar zcvf ファイル.tgz ディレクトリ )
混乱を避けるため,tgz のファイル名とディレクトリ名とは同じものにしておくと良い.
$ make dist ... $ ls .. ディレクトリ/ ファイル.tgz ...
コマンドtar の使用方法:
$ tar zcvf ファイル.tgz ディレクトリ
$ tar ztvf ファイル.tgz
$ tar zxvf ファイル.tgz
前回の練習問題の成果物 pendraw/ (2個のプログラム pendraw と pdcl のソースファイルを含むディレクトリ) に対して, Makefile を作成せよ.
実行例:
$ ls pendraw/ ... $ cd pendraw/ $ make distclean rm *.o rm pdcl pendraw $ ls Makefile img.c img.h main.c pdcl.c pen.c pen.h $ make all ... $ ls Makefile img.c img.h img.o main.c main.o pdcl pdcl.c pdcl.o pen.c pen.h pen.o pendraw $ make distclean $ make pdcl $ ls Makefile img.c img.h main.c pdcl pdcl.c pdcl.o pen.c pen.h $ make distclean $ make pendraw $ ls Makefile img.c img.h img.o main.c main.o pdcl.c pen.c pen.h pen.o pendraw $ make dist $ cd .. $ ls pendraw/ pendraw.tgz ...
提出:
収録内容:
$ tar ztvf pendraw.tgz pendraw/ pendraw/Makefile pendraw/img.c pendraw/img.h pendraw/main.c pendraw/pdcl.c pendraw/pen.c pendraw/pen.h