Compile Fortran using GCC¶
GCC (shorthand for 'GNU Compiler Collection') is a collection of compilers able to compile multiple different programming languages.
This page describes how to compile Fortran code using the GCC.
Procedure¶
0. Create a Fortran source file¶
You will need Fortran code to work on.
In this optional step, a file with a minimal Fortran program is created.
Create and write a Fortran source file called hello_world.f
:
In nano, write the Fortran program as such:
1. Load a GCC module¶
Load a recent GCC module:
Do I really need to load a module?
No, as there is a system-installed GCC.
For sake of doing reproducible research, always load a module of a specific version.
2. Compile the source file¶
After saving and closing nano, compile as such:
This compiles the file hello_world.f
using all defaults:
- default/no optimization
- the executable created is called
a.out
To compiles the file hello_world.f
with run-time speed optimization
and creating an executable with a more sensible name, use:
-Ofast
: optimize for run-time speed, similar to-O3
-o hello_world
: the executable created is calledhello_world
3. Run¶
Run the program:
Output: