Search code examples
makefilecompiler-flags

Makefile: different flags for similar files


I have several files to compile: one.f two.f ... ten.f

Some need to be compiled with different flags. For example, from one.f to five.f need to be compiled with ifort -O and six.f to ten.f need to be compiled with ifort -O2.

How can I make two implicit rules that know which files compile with which flags? Or any way in which to group then together so I don't have to specify file by file.

Similarly:

Some depend on the module mod.f and others don't. Can I specify this dependency without doing it in a file by file basis?


Solution

  • You can absolutely specify dependencies on a per-target basis but you do need a way of specifying the right targets (likely explicitly) though not necessarily manually if you can construct the list some other way (or have it in a variable already).

    The line one.o two.o three.o four.o five.o: mod.f will add that as a prerequisite to those targets (for whatever rule actually builds those targets.

    Similarly you can assign 6.11 Target-specific variables and use those.

    one.o two.o three.o four.o five.o: FFLAGS+=-O
    six.o seven.o eight.o nine.o ten.o: FFLAGS+=-O2
    

    will add the right optimization flag to the built-in %.o: %.f (or anything else that uses $(FFLAGS).