From 3920ab81c7c6e51f4ef8d12946cce5cf453c7af4 Mon Sep 17 00:00:00 2001 From: rasmus Date: Fri, 7 Feb 2025 12:18:36 +0100 Subject: [PATCH] Finish Set Up page for Fortran --- .../Fortran/Fortran_Hello_World.vue | 31 ++-- src/components/Fortran/Fortran_Set_Up.vue | 137 ++++++++++++++++-- 2 files changed, 141 insertions(+), 27 deletions(-) diff --git a/src/components/Fortran/Fortran_Hello_World.vue b/src/components/Fortran/Fortran_Hello_World.vue index 4a8fa4b..f4160fc 100644 --- a/src/components/Fortran/Fortran_Hello_World.vue +++ b/src/components/Fortran/Fortran_Hello_World.vue @@ -6,16 +6,19 @@

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.

-
-

+    
+

 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.

@@ -26,20 +29,21 @@ end program hello 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.

-
-

+    
+

 program hello
   implicit none
 
@@ -54,8 +58,9 @@ program hello
   print *, trim(usertxt)
 
 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 @@ -81,14 +86,14 @@ end program hello 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
  Input text here:
 Hello world :D
  Hello world :D
-      
+
\ 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 87eb979..b206a3e 100644 --- a/src/components/Fortran/Fortran_Set_Up.vue +++ b/src/components/Fortran/Fortran_Set_Up.vue @@ -33,12 +33,12 @@

For Fedora.

- $ sudo dnf install gcc-gfortran + $ sudo dnf install gcc-gfortran

For ubuntu.

- $ sudo apt install gfortran + $ sudo apt install gfortran

@@ -49,12 +49,13 @@

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.

+ 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 +

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.


@@ -64,8 +65,8 @@ and we will let you know, when or how we set up the project.


-
-

+    
+

 project/
 ├── src/          # Source files (.f90, .f95, etc.)
 │   ├── modules/  # Module definitions
@@ -75,19 +76,127 @@ project/
 ├── bin/          # Executable binaries
 ├── tests/        # Test cases
 └── Makefile      # Build script
-      
+

Notes:


-

Makefiles are very useful, and becomes increasingly useful, the bigger the project gets.

+

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.

+
+ +

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.

+
+ + hello.f90 +
+

+program hello
+    implicit none
+    print *, 'Hello, World!'
+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.

+
+ + +
+ +

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 for hello.f90
+
+FC = gfortran  # Fortran compiler
+FFLAGS = -O2   # Compiler flags (optimization level)
+
+hello: hello.o
+    $(FC) $(FFLAGS) -o $@ $^
+
+hello.o: hello.f90
+    $(FC) $(FFLAGS) -c $<
+
+run:
+    ./hello
+
+clean:
+    rm -f *.o hello
+
+
+
+ +

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.

+
+ +
+

+$ ./hello
+ Hello, World!
+
+
+
+ +

The exact same should happen, if we run the next command instead.

+ +
+

+$ make run
+./hello
+ Hello, World!
+
+
+
\ No newline at end of file