Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Makefile Puzzle: Multiple Programming Languages

I have a simple test Makefile:

hello: hello.o
.SUFFIXES: .c .f90 .o
.f90.o:
    pgf90 -c -o $@ $<
.c.o:
    cc -c -o $@ $<

You don't have to tell me that it having a foo.c and a foo.f90 in the same directory will create confusion. I would never do this for real. I am simply trying to get an idea of how Make handles some precedents. So, when I simply issue the make command, make runs:

pgf90 -c -o hello.o hello.f90
cc hello.o -o hello

And of course, the "cc" link fails because "cc" can't link a FORTRAN object to make an executable. Fine. But my question is: I have tried everything I can think of to discourage make from using the pgf90 command as its first compile command, and to opt for the cc command instead. I have changed the order of the suffix rules. I have changed the order of the suffixes in the .SUFFIXES statement. The only thing that seems to work is leaving the .f90 suffix rule out altogether. Why is this and can it be changed? Thanks. (In case it doesn't go without saying, I do have simple hello.f90 and hello.c source files in my directory, and they do compile and execute just fine.)

UPDATE: Ran make with -d. The relevant output (AFAICT) looks like:

Considering target file 'hello.o'.
 File 'hello.o' does not exist.
 Looking for an implicit rule for 'hello.o'.
 Trying pattern rule with stem 'hello'.
 Trying implicit prerequisite 'hello.c'.

Not being impressed with "implicit prerequisite 'hello.c'", make tries a whole bunch of other things before it comes to

 Found an implicit rule for 'hello.o'.
  Considering target file 'hello.f90'.
   Looking for an implicit rule with stem 'hello.f90'
    .
    .
    .
   No implicit rule found for 'hello.f90'.    #???????
    .
    .
 Must remake target 'hello.o'
pgf90 -c -o hello.o hello.f90

Curiouser and curiouser

like image 317
bob.sacamento Avatar asked Nov 14 '22 00:11

bob.sacamento


1 Answers

For reasons that are a mystery to me (but are probably obscure and historical), you can change this behavior by changing the suffix rules

.f90.o:
    pgf90 -c -o $@ $<
.c.o:
    cc -c -o $@ $<

into pattern rules (and reversing the order):

%.o : %.c
    cc -c -o $@ $<
%.o : %.f90
    pgf90 -c -o $@ $<
like image 89
Beta Avatar answered Dec 29 '22 00:12

Beta