How to Learn Fortran on a Mac
Fortran is a programming language designed primarily for use in writing programs capable of solving scientific problems. Although this was the primary purpose behind the creation of Fortran, it has become a mature language capable of general computer programming needs as well. You can learn Fortran on an Apple Mac system just as well as on any other computer running other operating systems. You will need to install a full-featured Fortran compiler on your Mac before beginning with any lessons or tutorials related to the Fortran language itself.
Instructions
-
-
1
Download and install the freely available GNU Fortran compiler (GFortran) onto your Apple Mac computer (see Resources). The GFortran compiler for Apple Mac comes in three versions: Intel version for Mac OS versions "Tiger" and later, Intel 64-bit version for Mac OS version "Snow Leopard," and Power PC Leopard version for those who are operating 64-bit PowerPC systems. Install the package just as with all other program installations on your Mac computer.
-
2
Configure GFortran so you are able to compile your Fortran programs from any file directory on your Mac system. To do so, enter the following into the "Terminal" window on separate lines. Quotation marks are used here only to separate the individual lines, and must not be included when typing them into the Terminal: "cd ~", "ls -al". Look for a file named ".bash_profile" in the resulting list. Open that file in TextEdit by issuing the following command: "open -e .bash_profile".
-
-
3
Type in a "path" statement into ".bash_profile" which is now opened up in TextEdit. The line must appear as follows, without quotes: "export PATH=/usr/local/bin:$PATH". This line must be entered as the last line in the file. Once entered, click "File" at the top of TextEdit and click "Save" to save the newly-modified .bash_profile. Close TextEdit and Terminal. You will now be able to use the "gfortran" compiler command from within any directory on your Mac.
-
4
Create a new directory on your Mac into your "Home" directory. The easiest way is to open "Terminal" once again and enter the following two lines, each separate: "cd ~" then "mkdir dirname" -- where dirname is the name you want the directory to be. It could be named "My Fortran" or anything similar. Save your Fortran source code files into this directory when working on tutorials, lessons and examples.
-
5
Open TextEdit once again. Click "File" at the top and create a new plain text file. Enter the following into the new document edit screen, and make sure each line is indented six spaces. "PROGRAM HIWORLD", "WRITE(UNIT=*, FMT=*) 'Hello World'", "END". In this example, each separate line is indicated using quotation marks. Do not enter the quotation marks into your program. Click "File" and then save the file as "hiworld.f77" into the new directory created earlier in this article.
-
6
Open "Terminal" and enter the directory you created in which to store your Fortran source code and compiled programs. First, go to your "Home" directory by typing "cd ~" on a separate line. On the next command prompt, enter "cd dirname" where dirname is the name of the directory folder you created earlier. Once you are in the directory, compile the program by typing the following in Terminal: "gfortran hiworld.f77 -o hiworld" and wait for your program to compile.
-
7
Run the example program in Terminal by simply typing "hiworld" and pressing the "Enter" key to execute it. At this point, you should now see the text "Hello World" displayed on a line in Terminal.
-
8
Work on more tutorials and lessons via the Internet, since there are many available for learning the Fortran language (see Resources). Remember to always use a ".f77" extension at the end of all Fortran source code text files. When compiling, use the "-o" flag symbol to tell the compiler what you want the compiled file named as, then provide that name immediately after the "-o" flag.
-
1
Tips & Warnings
GFortran is able to compile the newer versions of Fortran, which are Fortran 90 and Fortran 95. It would be easiest to learn Fortran 77 first since more examples exist for the older version. Once learned, the new features in Fortran 90 and Fortran 95 can be grasped more easily.
References
Resources
- Photo Credit Hemera Technologies/Photos.com/Getty Images