Search code examples
modulefortranbuild-process

Fortran 95 - How to automatically find modules used in the main program when compiling it


I have made my first Fortran programme mainp.f95, with two separate files mfun.f95 and mbisection.f95, which are the modules used in mainp.f95.

I have found on Internet 2 ways to compile it:

First way

gfortran mfun.95 mbisection.f95 mainp.f95

Then a.out and two files.mod are created, being a.out the executable.

Second way

gfortran -c mfun.95 -o mfun.o
gfortran -c mbisection.f95 -o mbisection.o

then two files.o and two files.mod are created.

gfortran mainp.f95 mbisection.o mfun.o

then the a.out file is created.

I do not know if they are optima, but they worked.

Question

However, imagine that I do not have 2 modules, but 20 modules.

Should I write each module in the gfortran command, or is there a way to "compile only the main programme", automatically adding the required .o and .mod files? (providing that all the files are in the same folder, therefore it is easy to "search the modules")

Thank you very much!!!


Solution

  • When a project grows from "Hello World" to something more complicated, it is time to switch from direct command line compilation to a build automation tool. There are many possibilities how to simplify building of large projects, for example makefiles, Microsoft Visual Studio, cmake.