Finish Set Up page for Fortran
This commit is contained in:
parent
a4b2d1378e
commit
3920ab81c7
@ -6,16 +6,19 @@
|
||||
<div>
|
||||
<h1 class="text-3xl">Hello World In Fortran 90</h1>
|
||||
<hr>
|
||||
<p></p>
|
||||
<p>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.</p>
|
||||
|
||||
<div class="font-mono">
|
||||
<pre><code class="text-sm">
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
program hello
|
||||
implicit none
|
||||
print *, 'Hello world!'
|
||||
end program hello
|
||||
</code></pre>
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>In the good ol' days (before FORTRAN 77), when printing to console, you would write <span><code>write(*,*)</code></span>,
|
||||
but with the release of FORTRAN 77, that became redundant, as you could use the newest keyword: <span><code>print</code></span>.</p>
|
||||
@ -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 <span><code>write(*,*)</code></span>
|
||||
in this example.</p>
|
||||
|
||||
<div class="font-mono">
|
||||
<pre><code class="text-sm">
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
$ gfortran -o hello main.f90
|
||||
$ ./hello
|
||||
Hello world!
|
||||
</code></pre>
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>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.</p>
|
||||
|
||||
<div class="font-mono">
|
||||
<pre><code class="text-sm">
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
program hello
|
||||
implicit none
|
||||
|
||||
@ -54,8 +58,9 @@ program hello
|
||||
print *, trim(usertxt)
|
||||
|
||||
end program hello
|
||||
</code></pre>
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>So what we're seeing here, is that we have created two variables: <span><code>usertxt</code> and <code>ios</code></span>.
|
||||
<span><code>usertxt</code></span> 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:</p>
|
||||
|
||||
<div class="font-mono">
|
||||
<pre><code class="text-sm">
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
$ gfortran -o hello main.f90
|
||||
$ ./hello
|
||||
Input text here:
|
||||
Hello world :D
|
||||
Hello world :D
|
||||
</code></pre>
|
||||
</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
@ -33,12 +33,12 @@
|
||||
<br>
|
||||
|
||||
<p>For Fedora.</p>
|
||||
<code>$ sudo dnf install gcc-gfortran</code>
|
||||
<code class="text-red-500">$ sudo dnf install gcc-gfortran</code>
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<p>For ubuntu.</p>
|
||||
<code>$ sudo apt install gfortran</code>
|
||||
<code class="text-red-500">$ sudo apt install gfortran</code>
|
||||
<br>
|
||||
<br>
|
||||
|
||||
@ -49,12 +49,13 @@
|
||||
<hr>
|
||||
|
||||
<p>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 <code>src</code> folder. That's where we keep all of out
|
||||
source code. Like <code>*.f90, *.f95</code> files.</p>
|
||||
layouts for the structure, but equal amongst them, is the <code class="text-red-500">src</code> folder. That's where we keep all of out
|
||||
source code. Like <code class="text-red-500">*.f90, *.f95</code> files.</p>
|
||||
<br>
|
||||
|
||||
<p>But other than that, most projects include a <code>Makefile</code> file, a <code>library</code>, <code>test</code>
|
||||
and <code>bin</code> folder. Your layout doesn't need to look like this specifically, nor does it have to contain
|
||||
<p>But other than that, most projects include a <code class="text-red-500">Makefile</code> file, a
|
||||
<code class="text-red-500">library</code>, <code class="text-red-500">test</code>
|
||||
and <code class="text-red-500">bin</code> 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.</p>
|
||||
<br>
|
||||
@ -64,8 +65,8 @@
|
||||
and we will let you know, when or how we set up the project.</p>
|
||||
<br>
|
||||
|
||||
<div class="font-mono">
|
||||
<pre><code class="text-sm">
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
project/
|
||||
├── src/ # Source files (.f90, .f95, etc.)
|
||||
│ ├── modules/ # Module definitions
|
||||
@ -75,19 +76,127 @@ project/
|
||||
├── bin/ # Executable binaries
|
||||
├── tests/ # Test cases
|
||||
└── Makefile # Build script
|
||||
</code></pre>
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>Notes:</p>
|
||||
<ul>
|
||||
<li>> <code>src</code> (This is where we keep all of out source code).</li>
|
||||
<li>> <code>modules</code> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
|
||||
<li>> <code>include</code> (This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).</li>
|
||||
<li>> <code>bin</code> (This is where the compiled program should end up in).</li>
|
||||
<li>> <code class="text-red-500">src</code> (This is where we keep all of out source code).</li>
|
||||
<li>> <code class="text-red-500">modules</code> (This is where we keep our classes. Or in Fortran-speak, modules).</li>
|
||||
<li>> <code class="text-red-500">include</code> (This is where we keep our interfaces, that out modules will inherit from, if we're using Fortran 90 or above).</li>
|
||||
<li>> <code class="text-red-500">bin</code> (This is where the compiled program should end up in).</li>
|
||||
</ul>
|
||||
<br>
|
||||
|
||||
<p>Makefiles are very useful, and becomes increasingly useful, the bigger the project gets.</p> <!-- TODO: Continue from here. Show example. -->
|
||||
<h1 class="text-3xl">Compiling</h1>
|
||||
<hr>
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<code class="text-red-500">hello.f90</code>
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
program hello
|
||||
implicit none
|
||||
print *, 'Hello, World!'
|
||||
end program hello
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<ul>
|
||||
<li>> <code class="text-red-500">hello: hello.o</code></li>
|
||||
<li class="pb-2">> > Here, the program "hello" depends on the object file "hello.o".</li>
|
||||
<li>> <code class="text-red-500">$(FC) $(FFLAGS) -o $@ $^</code></li>
|
||||
<li class="pb-2">> > This one compiles the object files into the program.</li>
|
||||
<li>> <code class="text-red-500">hello.o: hello.f90</code></li>
|
||||
<li class="pb-2">> > Here, the object file "hello.o" depends on the source file "hello.f90".</li>
|
||||
<li>> <code class="text-red-500">$(FC) $(FFLAGS) -c $<</code></li>
|
||||
<li class="pb-2">> > This one compiles the source files into the object files.</li>
|
||||
<li>> <code class="text-red-500">run: ./hello</code></li>
|
||||
<li class="pb-2">> > This runs the program if you execute <code class="text-red-500">make run</code></li>
|
||||
<li>> <code class="text-red-500">clean: rm -f *.o hello</code></li>
|
||||
<li class="pb-2">> > Here, the object files gets removed when running <code class="text-red-500">make clean</code></li>
|
||||
</ul>
|
||||
<br>
|
||||
|
||||
<p>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.</p>
|
||||
<br>
|
||||
|
||||
<code class="text-red-500">makefile</code>
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
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
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>Now simply run the makefile with the following command.</p>
|
||||
<br>
|
||||
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
$ make
|
||||
gfortran -O2 -c hello.f90
|
||||
gfortran -O2 -o hello hello.o
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>We have now successfully compiled the program with the help of a makefile. Now simply run the program and we'll
|
||||
see the output.</p>
|
||||
<br>
|
||||
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
$ ./hello
|
||||
Hello, World!
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
|
||||
<p>The exact same should happen, if we run the next command instead.</p>
|
||||
|
||||
<div class="font-mono shadow-xl p-2">
|
||||
<pre><code class="text-sm">
|
||||
$ make run
|
||||
./hello
|
||||
Hello, World!
|
||||
</code></pre>
|
||||
</div>
|
||||
<br>
|
||||
</div>
|
||||
</template>
|
Loading…
Reference in New Issue
Block a user