Table of Contents
Write and run your programs
By Yutaka Masuda
In the course, we will use a remote server to write and run (execute) your program. The files will not be saved in your laptop or local computers unless you transfer the files with WinSCP, Filezilla, or other methods. Please follow the steps.
- Connect to the server using Putty, Terminal, or your favorite terminal software.
- Move to a working directory.
- Write a code with a program editor.
- Compile your program with a compiler. If it is not successful, return to the edit.
- Execute the program. If you find problems, return to the edit.
Before programming, please prepare your environment as in a separate page, Setup Fortran Environment. This page also explains how to connect to the server.
Connect to the server
The first step
Using Putty or Terminal, connect to the server (running Linux). The server name, your account name, and the password will be given by lecturers. After the connection, you will see the following command prompt.
[ads-guest99@host2 ~]$
You are ready to type Linux/Unix commands here.
Hereafter, we will use $
as the prompt to show command-line examples.
For instance, the following command means: please type ls
and you don't have to type $
there.
$ ls
To do every time you log in
Whenever you newly login the server, please type the following command (change 99
as your account name).
$ cd /work/ads-guest99
In this working directory, you can freely create new files and directories.
Some useful commands
If you are not familiar with Linux or Unix-like system, please visit tutorial websites (like https://maker.pro/linux/tutorial/basic-linux-commands-for-beginners); you can find many options online. Here we show a few commands useful to finish the course.
pwd
: shows the name of the current working directoryls
: shows the list of file in the current working directoryll
: as above but in a list stylecp
: make a copy of your filesmv
: moves files to another location, or rename a fileexit
: logout the server
Edit a program file
Text editor
All program should be prepared as a text file, which is created and manipulated with a text editor.
Notepad (Windows) and TextEdit (Mac) are typical text editors but not available on the remote server with Linux.
If you have a favorite editor in Linux (vi, Emacs, nano, etc), please use it.
In this course, we recommend you to use gedit
to edit a file on the server.
Run gedit
Quick run
Make sure either Xming (Windows) or XQuartz (Mac) is running in your computer. For the first try, type the command in the terminal.
$ gedit prog1.f90
In this example, prog1.f90
is a file name for the Fortran program.
You can put any name on your file.
If successful, you will see a window to edit a file.
You can create a program in the editor, click the Save button, and close the window to come back the terminal. In this method, you can use either the terminal window or the editor window at the same time (and it is inconvenient).
More convenient way
If you invoke gedit with a sign &
, you can freely switch two windows (this sign run the editor as a background jobs).
$ gedit prog1.f90 &
This is useful because a sequence of programming (edit-compile-run-edit again) can be done by switching two windows.
Run vi or vim
If you are satisfied with gedit, please skip this section and move to the next, or you will get confused.
Linux has a traditional text editor, vi (actually vim installed in many systems). This editor runs in the terminal so it doesn't open new window (and you don't need Xming or XQuartz). The vi editor is designed to intensively use keyboard without graphical guidance so it looks cryptic for new users.
To invoke vi, type vi
with a file name in a command line.
$ vi prog1.f90
If you accidentally run vi, 1) hit Esc key once, 2) type :q
, and 3) then hit Enter key.
:q
The editor has 2 modes: command mode and edit mode. You have to switch the modes inside the editor to do what you want to do.
- When starting the editor, you are in the command mode which accept the command to action such as exit the program, save a file, search a text etc.
- To input the text, push a single key
i
(meaning insert) ora
(add) to move on the edit mode (no Enter key). - To save the file, push
Esc
key to come back to the command mode, type:w
(write), and hit the Enter key. - To edit the file again, push
i
ora
. - You have to move to the edit mode and come back to the command mode repeatedly to finish the program. Then type
:q
in the command mode to quit the program.
You should know which which model are you currently working.
Hints:
- If you get lost, hit the
Esc
key (on the top left of the keyboard) a few times so you will move to the command mode. - The simplest way to quit vi: hit the Esc key many times, type
:q
, and hit the Enter key.
Write, compile, and run the first program
Write a code
Let's write the first program on the server.
Invoke gedit to create a file prog1.f90
as a background job.
$ gedit prog1.f90 &
The example file has only 3 lines. After writing the code, push Save button to save it in the server.
Switch to the terminal window and hit the Enter key to get the focus.
Type ls
to see the file prog1.f90
is in the current directory.
$ ls prog1.f90
Compile the code
Let's compile it.
Compilation means the program you have written (source code) is converted to the machine code (executable program).
We have a compiler, named gfortran
(GNU Fortran Compiler), on the server.
We would use a different compiler so please ask the lecturer which compilers are available.
Call the compiler with your source file.
$ gfortran prog1.f90
If there is no problem, you will not get any message from the compiler. With some issues, the compiler displays a list of problems in your source code. When you get the error, please read the error message, go back to the editor, find the issues, and correct it. Then try the compilation again.
Run the program
After the successful compilation, you find an executable file a.out
in the same directory.
$ ls a.out prog1.f90
To run (execute) the program, please type the following command.
$ ./a.out
The program shows some message on the screen and exits.
Some compilation options
Change the output name
If you want to change the name of executable from the default a.out
to your favorable name, please use the -o
option (remember it as output).
It needs the name of output file.
The following example creates prog1
instead of a.out
.
$ gfortran prog1.f90 -o prog1
You can execute it.
$ ./prog1
Advanced: simple compilation
You don't need this feature for a simple program.
If you want to create a object file (simply compiled machine code), not the exacutable, please use -c
option (remember it as compilation).
The following example creates prog1.o
not a.out
.
This is useful when you compile modules and subroutines in a separate file.
$ gfortran -c prog1.f90