-
Data Types
-
+
Data Types
-
Just like any other programming language that has datatypes, Fortran has datatypes as well. The big difference, from
- other languages, of cause, is how they're created and assigned. If you're a developer of, for example, C#, Java or
- even Python, you will feel the difference pretty fast.
+ Just like any other programming language that has datatypes, Fortran has datatypes as well. The big difference, from
+ other languages, of cause, is how they're created and assigned. If you're a developer of, for example, C#, Java or
+ even Python, you will feel the difference pretty fast.
- Let's look at a small list of some common types.
-
+ Let's look at a small list of some common types.
-
- - >
integer
- - >
real
- - >
double precision
- - >
logical
- - >
character
-
+
+ - >
integer
+ - >
real
+ - >
double precision
+ - >
logical
+ - >
character
+
-
integer
is, if you hadn't guessed, a whole number. Like most languages,
- int
. The real
type is like a
- float
. But the difference with Fortran types, in general, is that you have "less" types,
- but you have more control over those types, making it essentially the same as if you had more types. More on that later.
+ integer is, if you hadn't guessed, a whole number. Like most languages,
+ int. The real type is like a
+ float. But the difference with Fortran types, in general, is that you have "less" types,
+ but you have more control over those types, making it essentially the same as if you had more types. More on that later.
- The double precision
is a double, obviously. And as you have probably guessed at
- this point, logical
is a boolean. character
- however, is a little bit different. In "new" programming terms, it would be an array of characters that behaves
- like a string. Or like in SQL: VARCHAR(256)
.
+ The
double precision is a double, obviously. And as you have probably guessed at
+ this point,
logical is a boolean.
character
+ however, is a little bit different. In "new" programming terms, it would be an array of characters that behaves
+ like a string. Or like in SQL:
VARCHAR(256).
-
integer
-
+
integer
+
-
In Fortran, you can specify the size of an integer at declaration. You do that with (kind = n)
.
- So if you want a 16 bit integer, you do integer(kind = 16)
. Observe the following code example.
- As an exercise for the reader, try to imagine what would be printed to the terminal.
+ In Fortran, you can specify the size of an integer at declaration. You do that with (kind = n).
+ So if you want a 16 bit integer, you do integer(kind = 16). Observe the following code example.
+ As an exercise for the reader, try to imagine what would be printed to the terminal.
- *Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.[1]
-
+ *Do note that we're using Fortran 90, and not the newer versions that supports unsigned integers.
[1]
+
-
main.f90
-
+
main.f90
+
program hello
implicit none
@@ -72,11 +70,11 @@ program hello
end program hello
-
+
-
Now, as the astute reader might have already figured, the output would look like this:
+
Now, as the astute reader might have already figured, the output would look like this:
-
+
> gfortran main.f90
> ./a.out
@@ -95,26 +93,25 @@ end program hello
kind = 16
170141183460469231731687303715884105727
-
+
-
The huge(n) function simply assigns the highest largest number of any integer,
- real or array type of any kind.
+ The huge(n) function simply assigns the highest largest number of any integer,
+ real or array type of any kind.
- Any integer without a kind, is automatically a 4 byte integer. Integers with kind 2, is 2 bytes big. Integers with
- kind 4 is 4 bytes big. And so on and so forth.
+ Any integer without a kind, is automatically a 4 byte integer. Integers with kind 2, is 2 bytes big. Integers with
+ kind 4 is 4 bytes big. And so on and so forth.
- Usually, unless your working with huge datasets, integer or if you want to be more semantic,
- integer(kind = 4) would be more than sufficient to work with.
-
+ Usually, unless your working with huge datasets,
integer or if you want to be more semantic,
+
integer(kind = 4) would be more than sufficient to work with.
-
real
-
+
real
+
-
The real type is, in most other languages, a float. And with
- integers, real can also be specified with a kind(n) modifier. Observe this example:
+
The real type is, in most other languages, a float. And with
+ integers, real can also be specified with a kind(n) modifier. Observe this example:
-
main.f90
-
+
main.f90
+
program hello
implicit none
@@ -142,11 +139,11 @@ program hello
end program hello
-
+
-
The output:
+
The output:
-
+
> gfortran main.f90
> ./a.out
@@ -166,19 +163,19 @@ end program hello
1.18973149535723176508575932662800702E+4932
3.36210314311209350626267781732175260E-4932
-
+
-
As you can see here, we have included a new print statement. tiny(n) returns
- the smallest positive number of a real kind. That function is not available for integers.
+
As you can see here, we have included a new print statement. tiny(n) returns
+ the smallest positive number of a real kind. That function is not available for integers.
-
double precision
-
+
double precision
+
-
double precision is pretty much just a bigger version of real. All of the same rules apply
- to double precision as the real type.
+
double precision is pretty much just a bigger version of real. All of the same rules apply
+ to double precision as the real type.
-
main.f90
-
+
main.f90
+
program hello
implicit none
@@ -191,11 +188,11 @@ program hello
end program hello
-
+
-
And the output looks like this:
+
And the output looks like this:
-
+
> gfortran main.f90
> ./a.out
@@ -203,24 +200,23 @@ end program hello
1.7976931348623157E+308
2.2250738585072014E-308
-
+
-
double precision does not support the kind modifier. A double precision number is
- also the same as an 8 byte real.
+
double precision does not support the kind modifier. A double precision number is
+ also the same as an 8 byte real.
-
logical
-
+
logical
+
-
logical is a boolean. It's either true or false. But a funny thing with the logical type in
- Fortran, compared to other languages, is that you can define the size of it. The logical type supports the
- kind(n) modifier.
+ logical is a boolean. It's either true or false. But a funny thing with the logical type in
+ Fortran, compared to other languages, is that you can define the size of it. The logical type supports the
+ kind(n) modifier.
- A good reason for the different sized logical, is optimization. Some systems handles larger object better than smaller,
- or some systems handles smaller objects better. It all depends on the architecture.
-
+ A good reason for the different sized logical, is optimization. Some systems handles larger object better than smaller,
+ or some systems handles smaller objects better. It all depends on the architecture.
-
main.f90
-
+
main.f90
+
program hello
implicit none
@@ -253,13 +249,13 @@ program hello
end program hello
-
+
-
Now I have a small exercise for the reader again. Let's assume that we are using gfortran
- without specifying an optimization level. What do you think the output would look like?
+
Now I have a small exercise for the reader again. Let's assume that we are using gfortran
+ without specifying an optimization level. What do you think the output would look like?
-
+
> gfortran main.f90
> ./a.out
@@ -282,18 +278,17 @@ end program hello
value: T
size: 16
-
+
-
Huh, that was weird. The 8 bit logical is false, while the others are true. The reason for this behavior
- could be a multitude of things. One could be optimization, as the compiler tries to make the program faster. And
- by setting the 8 bit logical to false. It could also be how an unassigned logical is represented. For more information,
- check the footnote number 2. [2]
+ Huh, that was weird. The 8 bit logical is false, while the others are true. The reason for this behavior
+ could be a multitude of things. One could be optimization, as the compiler tries to make the program faster. And
+ by setting the 8 bit logical to false. It could also be how an unassigned logical is represented. For more information,
+ check the footnote number 2. [2]
- A good programming practise is to always assign your logical when creating them.
+ A good programming practise is to always assign your logical when creating them.
-
-
main.f90
-
+
main.f90
+
logical :: bool = .true.;
logical(kind = 1) :: bool1 = .true.;
@@ -302,11 +297,11 @@ logical(kind = 4) :: bool4 = .true.;
logical(kind = 8) :: bool8 = .true.;
logical(kind = 16) :: bool16 = .true.;
-
+
-
Now, the output looks as expected.
+
Now, the output looks as expected.
-
+
> gfortran main.f90
> ./a.out
@@ -329,18 +324,18 @@ logical(kind = 16) :: bool16 = .true.;
value: T
size: 16
-
+
-
character
-
+
character
+
-
character in fortran can be seen as either a char or
- a string in other languages, depending on the length you give it.
+ character in fortran can be seen as either a char or
+ a string in other languages, depending on the length you give it.
- Observe the example below.
+ Observe the example below.
-
main.f90
-
+
main.f90
+
program hello
implicit none
@@ -360,11 +355,11 @@ program hello
end program hello
-
+
-
Output looks like this:
+
Output looks like this:
-
+
> gfortran main.f90
> ./a.out
@@ -378,24 +373,24 @@ end program hello
value: a lol
size: 10
-
+
-
lol. With Fortran, if you create a character, with a certain size, but only assign it a smaller vallue,
- the trailing indexes in the character will be filled with whitespace.
+ lol. With Fortran, if you create a character, with a certain size, but only assign it a smaller vallue,
+ the trailing indexes in the character will be filled with whitespace.
- To mitigate this, we could either set the correct length when creating the character, or we could use the trim(value)
- function. It removes trailing whitespaces from strings.
+ To mitigate this, we could either set the correct length when creating the character, or we could use the
trim(value)
+ function. It removes trailing whitespaces from strings.
-
main.f90
-
-
Now, the output should be fixed:
+
Now, the output should be fixed:
-
+
> gfortran main.f90
> ./a.out
@@ -413,16 +408,11 @@ trim(char2)//'lol';
value: alol
size: 10
-
-
-
Footnotes:
-
-
-
\ No newline at end of file
+
Footnotes:
+
+
\ No newline at end of file
diff --git a/src/components/Fortran/Fortran_Hello_World.vue b/src/components/Fortran/Fortran_Hello_World.vue
index c4baea7..9bc5bec 100644
--- a/src/components/Fortran/Fortran_Hello_World.vue
+++ b/src/components/Fortran/Fortran_Hello_World.vue
@@ -1,47 +1,48 @@
-
-
Hello World In Fortran 90
-
+
Hello World In Fortran 90
-
A Hello World program is always a great step into a new programming language. It gives a simple oversight into
- the build process of said language. And with a simple project come a simple intro (mostly). But fret not, Fortran
- can be simple or complex, depending on the size of the project.
+
A Hello World program is always a great step into a new programming language. It gives a simple oversight into
+ the build process of said language. And with a simple project come a simple intro (mostly). But fret not, Fortran
+ can be simple or complex, depending on the size of the project.
-
+
program hello
implicit none
print *, 'Hello world!'
end program hello
-
+
-
In the good ol' days (before FORTRAN 77), when printing to console, you would write write(*,*)
,
- but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: print
.
+ In the good ol' days (before FORTRAN 77), when printing to console, you would write write(*,*),
+ but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: print.
- With modern compilers, running the newest Fortran standards, write(*,*) 'Hello world!'
- and print *, 'Hello world!'
- will compile to the exact same assembly. So if you only need to print something to stdout, just use print *
- as it conveys the meaning of the code better. Plus we don't need the full functionality of write(*,*)
- in this example.
+ With modern compilers, running the newest Fortran standards,
write(*,*) 'Hello world!'
+ and
print *, 'Hello world!'
+ will compile to the exact same assembly. So if you only need to print something to stdout, just use
print *
+ as it conveys the meaning of the code better. Plus we don't need the full functionality of
write(*,*)
+ in this example.
-
+
$ gfortran -o hello main.f90
$ ./hello
Hello world!
-
+
-
Now, hear me out, why not spice up the program a little bit more? What if we want to read user input? Well, fret
- not my friend, because I have just the solution for you then. All we gotta do is to read from the terminal, and
- then print out the value.
+
Now, hear me out, why not spice up the program a little bit more? What if we want to read user input? Well, fret
+ not my friend, because I have just the solution for you then. All we gotta do is to read from the terminal, and
+ then print out the value.
-
+
program hello
implicit none
@@ -58,31 +59,30 @@ program hello
end program hello
-
+
-
So what we're seeing here, is that we have created two variables: usertxt
- and ios
.
- usertxt
is the input we're reading from the terminal. Although since Fortran 90 doesn't
- have an explicit string type, doesn't mean we can't read or write text, as the character
- type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
- whim.
+ So what we're seeing here, is that we have created two variables: usertxt
+ and ios.usertxt is the input we're reading from the terminal. Although since Fortran 90 doesn't
+ have an explicit string type, doesn't mean we can't read or write text, as the character
+ type is just an array of 1000 characters long. It doesn't have to be 1000 characters, but it's what I chose on a
+ whim.
- The other variable, ios
, I probably don't need to introduce, as it's just an integer.
- But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
- use of it.
+ The other variable, ios, I probably don't need to introduce, as it's just an integer.
+ But what control-flow this value holds, I feel is quite important, as a lot of file-based business logic makes
+ use of it.
- Introducing, iostat
. AKA, Input/Output Status Specifier. It indicates the status of the
- I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
- condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
- has triggered yet.
+ Introducing, iostat. AKA, Input/Output Status Specifier. It indicates the status of the
+ I/O operation. If the integer is positive, it indicates an error code. If it's negative, it indicates an end of file
+ condition. If it's zero, then it does not indicate anything yet. No error, no End Of File, no particular condition
+ has triggered yet.
- So in this case, where we check if ios
is End Of File, then that means if we input an
- End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
- We use the trim()
function to remove any trailing whitespaces. That's because, if you
- make a character array longer than the actual text, it will be filled with whitespace after the text. So now the output
- looks like this:
+ So in this case, where we check if
ios is End Of File, then that means if we input an
+ End Of File key combination (Ctrl+D on Unix/Linux, and Ctrl+Z on Windows), then the program would stop immediately.
+ We use the
trim() function to remove any trailing whitespaces. That's because, if you
+ make a character array longer than the actual text, it will be filled with whitespace after the text. So now the output
+ looks like this:
-
+
$ gfortran -o hello main.f90
$ ./hello
@@ -90,6 +90,5 @@ $ ./hello
Hello world :D
Hello world :D
-
\ No newline at end of file
diff --git a/src/components/Fortran/Fortran_Index.vue b/src/components/Fortran/Fortran_Index.vue
index 180a98b..5292a4d 100644
--- a/src/components/Fortran/Fortran_Index.vue
+++ b/src/components/Fortran/Fortran_Index.vue
@@ -1,31 +1,28 @@
-
-
Fortran
-
-
Fortran (Formula Translation), is one of the ancient languages somehow still in use as of today. First introduced
- in the 1950s by a team at IBM, Fortran was designed specifically for scientific and engineering applications,
- where its ability to efficiently handle complex mathematical operations far outperformed rivaling languages at the
- time.
-
+
Fortran
-
- Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
- 2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
- in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
- and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.
-
+
Fortran (Formula Translation), is one of the ancient languages somehow still in use as of today. First introduced
+ in the 1950s by a team at IBM, Fortran was designed specifically for scientific and engineering applications,
+ where its ability to efficiently handle complex mathematical operations far outperformed rivaling languages at the
+ time.
- Table of contents:
-
- - > Set Up
- - > Hello World
- - > Data Types
- - > Method, Functions, Sub Routines and Calls
- - > Reading A File
-
-
+ Over the years, Fortran has undergone numerous updates and revisions, with the most recent standard being Fortran
+ 2023. Even with the popularity of newer languages, such as C++, Java, and Python, Fortran remains a popular choice
+ in many fields, including physics, chemistry, meteorology, and engineering, due to its performance. Even Nvidia
+ and Intel are still developing their compilers, as well as FOSS compilers such as gfortran and lfortran.
+
+ Table of contents:
+
+ - > Set Up
+ - > Hello World
+ - > Data Types
+ - > Method, Functions, Sub Routines and Calls
+ - > Reading A File
+
\ No newline at end of file
diff --git a/src/components/Fortran/Fortran_Reading_Files.vue b/src/components/Fortran/Fortran_Reading_Files.vue
index f137ed6..0e6ca41 100644
--- a/src/components/Fortran/Fortran_Reading_Files.vue
+++ b/src/components/Fortran/Fortran_Reading_Files.vue
@@ -2,18 +2,17 @@
import PageStart from "../../../tags/PageStart.vue";
import CodeTag from "../../../tags/CodeTag.vue";
+import Paragraph from "../../../tags/Paragraph.vue";
-
-
Reading A File In Fortran 90
+
Reading A File In Fortran 90
-
Reading files in high level programming languages is rather easy. I C#, you would just create a
- StreamReader object, and execute: streamReader.ReadLine() and bam,
- you've read the first line of the file. Now just check if the string returned is null, in a while loop, and you've
- read the whole file. Easy peasy.
+ Reading files in high level programming languages is rather easy. I C#, you would just create a
+ StreamReader object, and execute: streamReader.ReadLine() and bam,
+ you've read the first line of the file. Now just check if the string returned is null, in a while loop, and you've
+ read the whole file. Easy peasy.
- In Fortran 90, however, there is a little more than that. Not a lot more, to be honest, but there is a little more.
-
+ In Fortran 90, however, there is a little more than that. Not a lot more, to be honest, but there is a little more.
\ No newline at end of file
diff --git a/src/components/Fortran/Fortran_Set_Up.vue b/src/components/Fortran/Fortran_Set_Up.vue
index 3f05705..96f52b2 100644
--- a/src/components/Fortran/Fortran_Set_Up.vue
+++ b/src/components/Fortran/Fortran_Set_Up.vue
@@ -1,57 +1,58 @@
-
-
Setting Up The Environment
-
+
Setting Up The Environment
-
If you're familiar with C, then you would feel right at home with Fortran's build environment. There isn't really
- an "official" compiler for the language, only a standard, that has to be implemented by the compilers. But there
- are a lot of great compilers, that might as well be "official."
+ If you're familiar with C, then you would feel right at home with Fortran's build environment. There isn't really
+ an "official" compiler for the language, only a standard, that has to be implemented by the compilers. But there
+ are a lot of great compilers, that might as well be "official."
- The top listed compiler on Fortran's website, is the GNU Fortran compiler (gfortran). It's the one I will be using
- throughout the examples on the site. Unless stated otherwise. There are also some other notable compilers, that
- has some rather interesting qualities to them.
+ The top listed compiler on Fortran's website, is the GNU Fortran compiler (gfortran). It's the one I will be using
+ throughout the examples on the site. Unless stated otherwise. There are also some other notable compilers, that
+ has some rather interesting qualities to them.
-
Compilers:
-
- - > lfortran (This one can be used as an interactive
- compiler, supports parsing all of the 2018 standard syntax and also compile Fortran to WebAssemply).
- - > NVIDIA HPC SDK (This one comes with a whole lot of GPU
- accelerated libraries).
-
+
Compilers:
+
+ - > lfortran (This one can be used as an interactive
+ compiler, supports parsing all of the 2018 standard syntax and also compile Fortran to WebAssemply).
+ - > NVIDIA HPC SDK (This one comes with a whole lot of GPU
+ accelerated libraries).
+
-
But we will be using gfortran. Through the examples on this site, I will mainly be focusing on developing on Linux.
- First, we install the compiler.
+
But we will be using gfortran. Through the examples on this site, I will mainly be focusing on developing on Linux.
+ First, we install the compiler.
-
For Fedora.
-
$ sudo dnf install gcc-gfortran
+
For Fedora.
+
$ sudo dnf install gcc-gfortran
-
For ubuntu.
-
$ sudo apt install gfortran
+
For ubuntu.
+
$ sudo apt install gfortran
-
And that's it. Pretty easy.
+
And that's it. Pretty easy.
-
Project Structure
-
+
Project Structure
+
-
When creating a Fortran project, or any project in general, you want a structure. There is a lot of different
- layouts for the structure, but equal amongst them, is the src
folder. That's where we keep all of out
- source code. Like *.f90, *.f95
files.
+ When creating a Fortran project, or any project in general, you want a structure. There is a lot of different
+ layouts for the structure, but equal amongst them, is the src folder. That's where we keep all of out
+ source code. Like *.f90, *.f95 files.
- But other than that, most projects include a Makefile
file, a
- library
, test
- and bin
folder. Your layout doesn't need to look like this specifically, nor does it have to contain
- the same folders. Each project is different, and so are the requirements. But this layout is simple, and great
- for medium to large projects.
+ But other than that, most projects include a Makefile file, a
+ library, test
+ and bin folder. Your layout doesn't need to look like this specifically, nor does it have to contain
+ the same folders. Each project is different, and so are the requirements. But this layout is simple, and great
+ for medium to large projects.
- But if we're doing micro-projects, as in like, a hello world application, a test application, or a small tool,
- this layout is rather redundant, and in the examples on this site, we will only use it on bigger projects,
- and we will let you know, when or how we set up the project.
+ But if we're doing micro-projects, as in like, a hello world application, a test application, or a small tool,
+ this layout is rather redundant, and in the examples on this site, we will only use it on bigger projects,
+ and we will let you know, when or how we set up the project.
-
+
project/
├── src/ # Source files (.f90, .f95, etc.)
@@ -65,33 +66,31 @@ project/
-
Notes:
-
- - >
src
(This is where we keep all of out source code).
- - >
modules
(This is where we keep our classes. Or in Fortran-speak, modules).
- - >
include
(This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).
- - >
bin
(This is where the compiled program should end up in).
-
+
Notes:
+
+ - > src (This is where we keep all of out source code).
+ - > modules (This is where we keep our classes. Or in Fortran-speak, modules).
+ - > include (This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).
+ - > bin (This is where the compiled program should end up in).
+
+
Compiling
+
-
Compiling
-
+
Makefiles are very useful, and becomes increasingly useful, the bigger the project gets. A makefile simplifies the
+ compilation process, as all of the compile flags can be gathered inside a single file. It makes it possible to
+ dynamically change the compilation depending on the system architecture or config. It also removes the need to
+ manually compile and link your objects together, every time you make a change.
- Makefiles are very useful, and becomes increasingly useful, the bigger the project gets. A makefile simplifies the
- compilation process, as all of the compile flags can be gathered inside a single file. It makes it possible to
- dynamically change the compilation depending on the system architecture or config. It also removes the need to
- manually compile and link your objects together, every time you make a change.
+ And with a bigger project, comes a big count of source files. That's where a makefile helps a lot, as it handles
+ and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.
- And with a bigger project, comes a big count of source files. That's where a makefile helps a lot, as it handles
- and tracks all changes to each file, ensuring that it's only the changed files that gets re-compiled.
+ So let's see a makefile in action. Below code is a simple Hello World application, that we will use a makefile to
+ compile. Although it's worth noting, that a program this small, a makefile is wildly redundant. But it does show
+ how it's set up in a simple environment.
- So let's see a makefile in action. Below code is a simple Hello World application, that we will use a makefile to
- compile. Although it's worth noting, that a program this small, a makefile is wildly redundant. But it does show
- how it's set up in a simple environment.
-
-
-
hello.f90
-
+
hello.f90
+
program hello
implicit none
@@ -100,34 +99,9 @@ end program hello
-
-
The makefile, in the eyes of a modern programmer (C#, Python, JS, Etc) might look rather repulsive. But be not
- afraid. It will all make sense in a bit.
-
-
- - >
hello: hello.o
- - > > Here, the program "hello" depends on the object file "hello.o".
- - >
$(FC) $(FFLAGS) -o $@ $^
- - > > This one compiles the object files into the program.
- - >
hello.o: hello.f90
- - > > Here, the object file "hello.o" depends on the source file "hello.f90".
- - >
$(FC) $(FFLAGS) -c $<
- - > > This one compiles the source files into the object files.
- - >
run: ./hello
- - > > This runs the program if you execute
make run
- - >
clean: rm -f *.o hello
- - > > Here, the object files gets removed when running
make clean
-
-
-
-
A quick note. It's very important that whenever something is indented in a makefile, that it's indented with a tab,
- and not 4 spaces. It needs to be a tab character.
-
-
makefile
-
+
makefile
+
-Makefile for hello.f90
-
FC = gfortran # Fortran compiler
FFLAGS = -O2 # Compiler flags (optimization level)
@@ -145,34 +119,59 @@ clean:
-
Now simply run the makefile with the following command.
+
The makefile, in the eyes of a modern programmer (C#, Python, JS, Etc) might look rather repulsive. But be not
+ afraid, because at the end of the day, it's simply just some rules, that the compiler follows to compile your program.
-
+
+ - > hello: hello.o
+ - > > Here, the program "hello" depends on the object file "hello.o".
+
+ - > $(FC) $(FFLAGS) -o $@ $^
+ - > > This one compiles the object files into the program.
+
+ - > hello.o: hello.f90
+ - > > Here, the object file "hello.o" depends on the source file "hello.f90".
+
+ - > $(FC) $(FFLAGS) -c $<
+ - > > This one compiles the source files into the object files.
+
+ - > run: ./hello
+ - > > This runs the program if you execute make run
+
+ - > clean: rm -f *.o hello
+ - > > Here, the object files gets removed when running make clean
+
+
+
A quick note. It's very important that whenever something is indented in a makefile, that it's indented with a tab,
+ and not 4 spaces. It needs to be a tab character.
+
+
Now simply run the makefile with the following command.
+
+
$ make
gfortran -O2 -c hello.f90
gfortran -O2 -o hello hello.o
-
+
-
We have now successfully compiled the program with the help of a makefile. Now simply run the program and we'll
- see the output.
+
We have now successfully compiled the program with the help of a makefile. Now simply run the program and we'll
+ see the output.
-
+
$ ./hello
Hello, World!
-
+
-
The exact same should happen, if we run the next command instead.
+
The exact same should happen, if we run the next command instead.
-
+
$ make run
./hello
Hello, World!
-
\ No newline at end of file