fortranexternalfujitsu-fortran

Fortran unable to call external library


I am trying to use an external library called "decomp_2d" with my fortran program. I have an environment variable declared "DECOMP2D" which points to the directory where "decomp_2d" is located. This directory has the following structure:

[k00603@fe01p03 2decomp_fft]$ls
doc  examples  include  lib  Makefile  README  src
[k00603@fe01p03 2decomp_fft]$ls lib/
lib2decomp_fft.a  Makefile
[k00603@fe01p03 2decomp_fft]$ls include/
decomp_2d_fft.mod  decomp_2d_io.mod  decomp_2d.mod  glassman.mod

In another directory I am trying to call a program which uses the subroutines of this library. But when I am doing this, I am getting a compile time error. I am attaching a minimal fortran code which I am using and the Makefile I am using to compile. It is a Fujitsu machine and they have there own fortran compiler which I am using to compile:

The program:

  program   Sora_v71

! Use the external library
  use decomp_2d

  integer n

  call decomp_2d_init(n,n,n,n,n)

  stop
  end

Makefile:

# Lines included for using the 2decomp libraries
INC_2DECOMP = -I$(DECOMP2D)/include/
L2DECOMP = -L$(DECOMP2D)/lib/ -l2decomp_fft

## ------------------------------------------------------
RM        =  rm
SRCDIR    = .
LIBDIR    = .
BIN       = binary
OBJS      = main.o 

## -------------------------------------------------------
mpifrtpx=$(shell which mpifrtpx)
FC=$(mpifrtpx)

FFLAGS   = $(F90FLAG) $(INC_2DECOMP) 
LFLAGS   = $(F90FLAG) -L$(LIBDIR) $(L2DECOMP)

## -------------------------------------------------------
all: $(BIN)

$(BIN): $(OBJS)
    @echo Linking $(BIN) .....
    $(FC) $(LFLAGS) -o $@ $(OBJS)

.f.o:
@echo Compiling $*.f
$(FC) $(FFLAGS) -c $(SRCDIR)/$*.f

clean:
@echo 'Cleaning .....'
$(RM) -f core *.o *~ *.L *.O $(BIN) $(SIZE_FILE)

When I am typing "make" I am getting the following error:

[k00603@fe01p04 test]$make
Compiling main.f
/opt/FJSVtclang/GM-1.2.0-11/bin/mpifrtpx  -I/home/hp120242/k00603/2decomp_fft//include/ -c ./main.f
Linking binary .....
/opt/FJSVtclang/GM-1.2.0-11/bin/mpifrtpx  -L. -L/home/hp120242/k00603/2decomp_fft//lib/ -l2decomp_fft -o binary main.o 
main.o: In function `MAIN__':
main.f:(.text+0x4c): undefined reference to `decomp_2d.decomp_2d_init_'
main.o:(.data+0x0): undefined reference to `decomp_2d.decomp_2d_init_'
make: *** [binary] Error 1

Any ideas?


Solution

  • With some linkers the order of the libraries in the linking command matters. Generally they have to come after the object files that reference them. Try to switch $(LFLAGS) and $(OBJS) in the link command.