ポイントは
- make のデフォルトルールをできるだけ活用する
- mod の依存関係をただしく反映する
でも実際はモジュールが再コンパイルされても .mod ファイルが更新されるとは限らない。そのときはそのモジュールを use しているファイルは再コンパイルの必要がないはずだ。
サンプルを示すよ。ターゲット sample は main.o と sub.o をリンクしてて、どちらも Fortran のプログラム。そして main.f90 はモジュール sub を use している。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program main | |
use sub | |
call helloworld | |
end program main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FC = gfortran | |
TARGET = sample | |
OBJS = main.o sub.o | |
.SUFFIXES: .f90 | |
%.o: %.f90 | |
$(COMPILE.f) $(OUTPUT_OPTION) $< | |
%.mod: %.f90 %.o | |
@: | |
$(TARGET): $(OBJS) | |
$(LINK.f) $^ $(LOADLIBES) $(LDLIBS) -o $@ | |
main.o: sub.mod |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module sub | |
use, intrinsic :: iso_fortran_env | |
contains | |
subroutine helloworld | |
write(output_unit, '(*(g0))') 'Hello World!' | |
end subroutine helloworld | |
end module sub |
こうしておくとオブジェクト間の依存関係を Makefile の最後の行のように素直に書くことができる。main.o は sub.mod に依存し sub.o には依存していない。
これで sub.f90 が更新されて sub.o が再作成されても sub.mod に変更がなければ main.o が再コンパイルされることはない。ただし sub.mod のみを削除するのは禁止な。
参考
http://d.hatena.ne.jp/pyridoxin/20110726/1311691190
http://lagrange.mechse.illinois.edu/f90_mod_deps/