2
votes

I have programmed some fortran subroutines. I saved the subroutines in a Desktop folder called subroutines. When I compile a fortran program, say main.f90 (located in a different folder than subroutines), that uses subroutines (e.g. sub1.f90, sub2.f90) of the folder subroutines, I need to copy every subroutine into the folder of main.f90 and then compile like this:

gfortran -o main main.f90 sub1.f90 sub2.f90

which outputs the executable file as desired.

My question is: Is there any compiler option in gfortran to include files from the subroutine folder without having to copy each subroutine to the folder of main.f90?

I have tried these two options:

gfortran -L/home/user/Desktop/subroutines -o main main.f90 sub1.f90 sub2.f90

gfortran -I/home/user/Desktop/subroutines -o main main.f90 sub1.f90 sub2.f90

both of these compiler options return the error: gfortran: No such file or directory

The only available fortran compiler in my PC is gfortran (no ifort).

1
You are almost certainly better off compiling each .f90 file separately and then linking finally. The -I flag relates to the search path for include files and module files.francescalus
Youbare probably looking for some build system, such as make or some better modern alternative.Vladimir F

1 Answers

1
votes

You can simply put the full path to the subroutine file. So you would augment your compilation as

gfortran -o main main.f90 subroutines/sub1.f90 subroutines/sub2.f90 

or a clever way to do this call is to use expansions as so

gfortran -o main main.f90 subroutines/{sub1,sub2}.f90 

where the curly brackets will expand to be the same as the first complication I show.