Linking library in fortran finite element analysis

Hello Fortran experts here,

I am playing around with a book called " programming finite element method". I have difficulty to link two library to some source codes.

1 there are some source files that you could run with G95 compiler
2 these source files all need two library: main and geom. they all start with
use main
use geom
3 I go to folder c/5th_ed/source/library/main
run: g95 -c *.f03
convert the .f03 file to object file .o
ar -r mainlib.a *.o to build the mainlib.a library

I did the same to geom library: created geomlib.a library

4 when I go to source code folder: c/5th_ed/source/chap04 to run some source code
by: g95 -o p41.f03
it gives error message :slight_smile: Fatal Error: Can’t open module file ‘main.mod’ at (1) for reading: No such file or direc
5. when I use: g95 -o p41.f03 -L/c/5th_ed/source/library/main mainlib
it gives error message: g95: mainlib: No such file or directory

you can see my difficulty is to help the compiler to find mainlib.a.

I am using cmd in windows to do above. is there any way that the compiler to find the library?

this is my main difficuty right now: how to link the external library to my source code.

Hope someone could point out some tricks to do it.

Many thanks

Martin

Welcome to the forum. Could you try running the same commands but with gfortran instead of g95? The g95 compiler is old and no longer maintained. I think there are many more people here using gfortran than g95, so if you have problems with gfortran, it will be easier for others to reproduce them. Admittedly, the author’s code site does have a g95 compiler binary, so the code should be compilable with g95.

main and geom are modules so you need definately need main.mod and geom.mod available when you compile in source code folder: c/5th_ed/source/chap04.

As you describe main and geom as “libraries” they may also contain routines, so you will need something like main.o and geom.o when linking all the .o files. main.o and geom.o may have been grouped into mainlib.a, so use this when linking.

Hi Beliavsky,

much appreciate your quick reply and suggestions.
I go into the source code directory
C:\5th_ed\source\chap04>
and run gfortran
gfortran -o p41.f03 -L/c/5th_ed/source/library/main lmainlib
and it says it could not find mainlib.a library
gfortran: error: lmainlib: No such file or directory

the source code folder and the library folder are located:
c/5th_ed/source/library
c/5th_ed/source/chap04~chap11
surprisingly, even after I copied mainlib.a and geomlib.a in chap04 folder, and run,
it still says it could not find mainlib.a and geonlib.a

is it amazing?

Thanks
Martin

Try

g95 -o p41.f03 /c/5th_ed/source/library/main/mainlib.a

Also why are you using g95. As far as I know, it hasn’t been supported in years. Look into using gfortran, Intel oneAPI, NVIDIA HPC toolkit (PGI compilers) which are all free to use, or the trial versions of NAG or Simply Fortran.

Hi Ramsu,

the error persists, without -L, the compiler could not locate the main directory.
it still could not find mainlib.a

C:\5th_ed\source\chap04>g95 -o p41.f03 /c/5th_ed/source/library/main mainlib.a
g95: error: /c/5th_ed/source/library/main: No such file or directory
g95: error: mainlib: No such file or directory

Thanks

Martin

Hi all,

Thanks for so many responses. I really appreciate your time.

the author has a build batch file to do the complication and link. see below

Build batch file:

echo off

rem EDIT THE NEXT TWO LINES IF NECESSARY*****

set ED5=C:\5th_ed

set G95=C:\g95

rem *****************************************************

set FEL=%ED5%\source\library

set PATH=%G95%\bin

rem BUILD LIBRARIES**************************************

cd %FEL%\geom

del *.a *.mod

g95 -c *.f03

ar -r geomlib.a *.o

del *.o

cd %FEL%\main

del *.a *.mod

g95 -c *.f03

ar -r mainlib.a *.o

del *.o

rem *****************************************************

rem BUILD runs.exe **************************************

cd %ED5%\source

g95 runs.f03 -o runs.exe

copy runs.exe %ED5%\source\chap04

copy runs.exe %ED5%\source\chap05

copy runs.exe %ED5%\source\chap06

copy runs.exe %ED5%\source\chap07

copy runs.exe %ED5%\source\chap08

copy runs.exe %ED5%\source\chap09

copy runs.exe %ED5%\source\chap10

copy runs.exe %ED5%\source\chap11

del runs.exe

rem *****************************************************

rem BUILD run5.bat *************************************

echo echo off>>run5.bat

I was able to build the two libraries mainlib.a and geomlib.a but could not go further using
normal compiling method you saw in prevous post.

in the build batch file, the author invoke runs.f03 to link the library and run the source code.
the runs.f03 code is as follows:

runs.f03

PROGRAM run
IMPLICIT NONE
INTEGER::narg
INTEGER::nlen
INTEGER::iargc
CHARACTER(LEN=15)::progname,dataname
LOGICAL found
narg=iargc()
IF(narg.lt.1)THEN
WRITE(,)‘Please enter the base name of program file: ’
READ(,) progname
WRITE(,)‘Please enter the base name of data file: ’
READ(,) dataname
ELSEIF (narg.lt.2)THEN
CALL getarg(1,progname)
WRITE(,)‘Please enter the base name of data file: ’
READ(,) dataname
ELSE
CALL getarg(1,progname)
CALL getarg(2,dataname)
ENDIF
nlen=lnblnk(progname)
INQUIRE(file=progname(1:nlen)//’.f03’,exist=found)
IF(.not.found)THEN
WRITE(,)‘Program file not found: ‘,progname(1:nlen)//’.f03’
WRITE(,)‘Please create or check spelling.’
STOP
ENDIF
nlen=lnblnk(dataname)
INQUIRE(file=dataname(1:nlen)//’.dat’,exist=found)
IF(.not.found)THEN
WRITE(,)‘Data file not found: ‘,dataname(1:nlen)//’.dat’
WRITE(,)‘Please create or check spelling.’
STOP
ENDIF
call system('run5.bat ‘//progname//’ '//dataname)
STOP
END PROGRAM run

I am trying to do this manually with the g95 or gfortran -c with explicit library path without success.

the task is to use g95 or gfortran compilations to do what runs.f03 does. this seems elude me.

Thanks

Martin

Hi John,
under main folder, there are around 40 .f03 files, under geom folder, there are 13 .f03 files.

if main.o and geom.a are grouped in mainlib.a and geomlib.a, anything different I should do
other than list the two libraries:
g95 -o p41.f03 -L/5th_ed/source/library geomlib mainlib
this one does not work .

thanks

Martin

Hi @Martiny . I have that book with me and I also used and extended some codes from that.
Best solution I found is -
go to main folder - copy all the files into a single file using the command:

copy *.f03 main.f03

Do the same for geom folder:

copy *.f03 geom.f03

Convert main.f03 and geom.f03 into modules - by write this in the main.f03:

module main
contains
.....
.....
All the copied subroutines come here
.....
last line -> end module main

You do the same thing for geom.f03 also.
Now you can move these two files to whatever “chapxx” folder you are working with and link easily:

g95/gfortran/ifort p41.f03 main.f03 geom.f03

I hope this helps !

Not answering the OP’s question, but I will make a general comment that Fortran source files should not have the suffix .f03 (which I assume stands for Fortran 2003). Because Fortran 90 introduced free source form the convention is to use the .f90 suffix for any source file using free format, even if it uses features from a standard later than Fortran 90.

2 Likes

The files “that all start with” this are looking for files main.mod and geom.mod. These .mod files would probably be generated when main and geom are compiled, and need to be accessible to the compiler when compiling these files, either copied to the directory or in a search path list.

This is basically how module information is transferred between files when they are compiled.
The .mod files must be generated before the “use” statement is compiled.

This is the basics of using a Fortran compiler for MODULEs and does not relate to the FE analysis. You are seeing this problem because the source code (.f90, .f03) and generated .mod files are in multiple directories.

it is great to hear that you have better success with the book and code.

it has been a nightmare for me.

I tried your method, the screen becomes sea of red.
C:\5th_ed\source\chap04>g95 -c p41.f03 main.f03 geom.f03
In file p41.f03:6

USE main
1
Fatal Error: Can’t open module file ‘main.mod’ at (1) for reading: No such file or directory
In file main.f03:1

moudle main
1
Error: Unclassifiable statement at (1)
In file main.f03:1058

end subroutine chobk2subroutine cholin(kb)
1
Error: Expected label ‘chobk2’ for END SUBROUTINE statement at (1)
In file main.f03:1060

implicit none
1
Error: Unexpected IMPLICIT NONE statement at (1)
In file main.f03:1061

INTEGER,PARAMETER::iwp=SELECTED_REAL_KIND(15)
1
Error: Symbol ‘iwp’ at (1) already has basic type of INTEGER
In file main.f03:1062

REAL(iwp),intent(in out)::kb(:,:slight_smile:
1
Error: Symbol ‘kb’ at (1) already has basic type of REAL
In file main.f03:1063

integer::i,j,k,l,ia,ib,n,iw
1
Error: Symbol ‘i’ at (1) already has basic type of INTEGER
In file main.f03:1064

REAL(iwp)::x
1
Error: Symbol ‘x’ at (1) already has basic type of REAL
In file main.f03:1088

end subroutine cholin
1
Error: Expected label ‘chobk2’ for END SUBROUTINE statement at (1)
In file main.f03:1089

strange.

Martin

the command to make library by ar -r manlib.a will generate two files
one is main.mod and mainlib.a ; same for geom.

the problem, as you said, is to pass this information to compiler.

even if I put these 4 files: two mod file and two lib file in the source file folder, it could not find
them.

it is so strange.

Martin

Hi Ashok,

Good to know that you have been successful with the book and code.

I tried your suggestion, receive a lot of error message.

I will give it another go and see what happens.

Thanks

Martin

I finally had some time to take another look at this and was able to get the code to compile with latest Intel oneAPI ifort on my Linux system doing the following.

  1. Use the following bash script to convert all the files with f03 extensions to f90. ifort seems to only support the .f, .F, .f90 and .F90 file extensions by default. I presume there is a way around this
#!/usr/bin/env bash
#chgext script
ext1=$1
ext2=$2
for file in *.$ext1
do
 mv "$file"  "${file%.$ext1}.$ext2"
done
exit
  1. run the script in geom and main and do the following

geom
chgext f03 f90
ifort -c *.f90
ar -rvs geomlib.a *.o
ranlib *.o

main
chgext
ifort -c *.f90
ar -rvs mainlib.a *.o
ranlib *.o

make lib and modules directories and copy .mod and .a files into respective directories

for chap04 files
cd chap04
chgext f03 f90
ifort -o p41.x -I …/modules p41.f90 …/lib/mainlib.a …/lib/geomlib.a …/lib/arpacklib.a

Note ifort does not require the -L although its probably a good practice to include it. I’ve found that -L libdirpath -l lib only works reliably with shared objects not archives.

Hi Rwmsu,

Fantastic. I will try the same thing on cygwin, hope it run smoothly.

I appreciate that you spend your personal time helping me out.

This forum is really helpful…

Thanks

Martin