Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a FORTRAN makefile

I have a FORTRAN source code consisting of many different .F and .h files. I need to build an executable from it, but I'm having some problems. The makefile that I produced so far (which may have errors as I'm new to this) is:

 # compiler
 FC = /usr/bin/gfortran-4.5

 # compile flags
 FCFLAGS = -g -c -fdefault-real-8 -fbacktrace -fno-align-commons
 # link flags
 FLFLAGS = -g -fbacktrace

 # source files and objects
 SRCS = $(patsubst %.F, %.o, $(wildcard *.F)) \
        $(patsubst %.h, %.mod, $(wildcard *.h))

 # program name
 PROGRAM = blah

 all: $(PROGRAM)

 $(PROGRAM): $(SRCS)
    $(FC) $(FCFLAGS) $@ $<

 %.o: %.F
    $(FC) $(FLFLAGS) -o $@ $<

 %.mod: %.h
    $(FC) $(FLFLAGS) -o $@ $<

 clean:
    rm -f *.o *.mod

When I try to make the program, however, I'm getting a slew of undefined reference errors. I mean, every function and subroutine call in the very first compiled .F file gives back an undefined reference error. I thought this was because gfortran was trying to link the files instead of just compiling them and then linking at the end, but I thought the '-c' option was supposed to prevent that.

UPDATE:

As commenters have pointed out, I mixed up the compile and link flags. Furthermore, you shouldn't compile *.h files. Here is the latest, corrected makefile:

# compiler
FC = /usr/bin/gfortran-4.4

# compile flags
FCFLAGS = -g -c -fdefault-real-8 -fbacktrace -fno-align-commons -fbounds-check -std=legacy
# link flags
FLFLAGS =

# source files and objects
SRCS = $(patsubst %.F, %.o, $(wildcard *.F))

# program name
PROGRAM = blah

all: $(PROGRAM)

$(PROGRAM): $(SRCS)
    $(FC) $(FLFLAGS) -o $@ $<

%.o: %.F
    $(FC) $(FCFLAGS) -o $@ $<

clean:
    rm -f *.o *.mod

Now when I run make, it will compile each *.F file in the code, but it fails at the linking stage. I get a bunch of undefined reference errors in the very first *.F file. The compiler seems to be going over each *.F file individually in the linking stage, which I'm not sure is correct. Then I get an error:

/usr/lib/gcc/x86_64-linux-gnu/4.4.5/libgfortranbegin.a(fmain.o): In function `main':
(.text+0x26): undefined reference to `MAIN__'
collect2: ld returned 1 exit status

However, if I type the command:

gfortran -o blah *.o

The executable will be built, so it seems like I did something wrong in the makefile for the linking stage.

UPDATE: 5/9/2011

Sverre pointed out the final problem with my makefile. In my first target that builds the program, I use the shortcut command for only the first dependency ($<), but I need to include all dependencies (i.e. all *.o files) using the ($^) shortcut. The final, working makefile is as follows:

# compiler
FC      := /usr/bin/gfortran-4.5

# compile flags
FCFLAGS = -g -c -fdefault-real-8 -fbacktrace -fno-align-commons -fbounds-check
# link flags
FLFLAGS =

# source files and objects
SRCS = $(patsubst %.F, %.o, $(wildcard *.F))
#       $(patsubst %.h, %.mod, $(wildcard *.h))

# program name
PROGRAM = vipre

all: $(PROGRAM)

$(PROGRAM): $(SRCS)
    $(FC) $(FLFLAGS) -o $@ $^

%.o: %.F
    $(FC) $(FCFLAGS) -o $@ $<

# %.mod: %.h
# $(FC) $(FCFLAGS) -o $@ $<

clean:
    rm -f *.o *.mod
like image 746
Bob Avatar asked May 03 '11 15:05

Bob


People also ask

What is a Fortran makefile?

A file called makefile tells make in a structured manner which source and object files depend on other files. It also defines the commands required to compile and link the files.

How do I create an executable file in Fortran?

The simplest way to build an application is to compile all of your Intel® Fortran source files and then link the resulting object files into a single executable file. You can build single-file executables using the ifort command from the command line.


1 Answers

Are you using GNU make? If so,

$(FC) $(FLFLAGS) -o $@ $<

may be the culprit. $< is the name of the first prerequisite, but you want all the *.o files. Try using $^ instead.

like image 162
sverre Avatar answered Sep 22 '22 01:09

sverre