user1326058
user1326058

Reputation:

Compiling Fortran 77 with an external library using a Makefile

I have main program Engine.f that calls functions/external in LIB.f. Unlike C++ and Java there is no include in the main program so it will be possible to compile.

How does my Fortran comiler know that there is another library which I use?

I'm using photran from Eclipse.

The MAKE file:

.PHONY: all clean

# Change this line if you are using a different Fortran compiler
FORTRAN_COMPILER = gfortran

all: src/Engine.f
    $(FORTRAN_COMPILER) -O2 -g \
        -o bin/Engine.exe \
        src/Engine.f

clean:
    rm -f bin/Engine.exe *.mod

errors that I get when I compile:

undefined reference to (name of function in **LIB.f**)

Upvotes: 0

Views: 706

Answers (1)

Pierre de Buyl
Pierre de Buyl

Reputation: 7293

.PHONY: all clean
all: Engine.exe

# Change this line if you are using a different Fortran compiler
FORTRAN_COMPILER = gfortran
FORTRAN_FLAGS=-O2 -g

%.o: src/%.f
    $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) -c $<

Engine.exe: Lib.o Engine.o
    $(FORTRAN_COMPILER) $(FORTRAN_FLAGS) \
        -o bin/Engine.exe \
        Lib.o Engine.o

clean:
    rm -f *.o *.mod

In FORTRAN 77, the compiler "just" needs the function to be supplied in a .o file at link time. You can test the Makefile below, it should do what you want.

Modern versions of Fortran use module files to structure libraries, if you ever upgrade to that.

Upvotes: 1

Related Questions