If you are used to programming in C/C++, Java or Pascal, you probably do this using an Integrated Development Environment (IDE’s) such as Dev-Cpp/Pascal, Netbeans, Eclipse, … There are dozens of free IDE’s for each of these languages. When starting to use fortran, you are in for a bit of a surprise. There are some commercial IDE’s that can handle fortran (MS Visual Studio, or the Lahey IDE). Free fortran IDEs are rather scarce and quite often are the result of the extension of a C++ focused IDE. This, however, does not make them less useful. Code::Blocks is such an IDE. It supports several programming and scripting languages including C and fortran, making it also suited for mixed languages development. In addition, this IDE has been developed for Windows, Linux and Mac Os X, making it highly portable. Furthermore, installing this IDE combined with for example the gcc compiler can be done quickly and without much hassle, as is explained in this excellent tutorial. In 5 steps everything is installed and you are up and running:
- Get a gfortran compiler at https://gcc.gnu.org/wiki/GFortran
Go for binaries and get the installer if you are using Windows. This will provide you with the latest build. Be careful if you are doing this while upgrading from gfortran 4.8 to 4.9 or 4.10. The latter two are known to have a recently fixed compiler-bug related to the automatic finalization of objects. A solution to this problem is given in this post.
UPDATE 03/02/2017: As the gcc page has changed significantly since this post was written, I suggest to follow the procedure described here for the installation of a 64bit version of the compiler.
- Get the Code::Blocks IDE at http://www.codeblocks.org/ or http://cbfortran.sourceforge.net/ (preferred)
Since version 13.12 the Fortranproject plugin is included in the Code::Blocks installation.
- Setup gfortran
Run the installer obtained at step 1…i.e. keep clicking OK until all is finished.
- Setup Code::Blocks for fortran
- Run the installer or Unzip the zip-file obtained in step 2.
- Run Code::Blocks and set your freshly installed GNU fortran compiler as default.
- Associate file types with Code::Blocks. If you are not using other IDE’s this may be an interesting idea
- Go to settings, select “Compiler and Debugger”, click on “Toolchain executables” and set the correct paths.
- Code::blocks has been configured.
- Your first new fortran program
- Go to “File” → “New” → “Project”.
- Select “Fortran Application”.
- Follow the Wizard: provide a project folder and title.
- Make sure the compiler is set to “GNU Fortran Compiler”, and click Finish.
- A new project is now being created, containing a main file named “main.f90”
- Click “Build”, to build this program, and then “Run”.
- Congratulations your first Fortran program is a fact.
Of course any real project will contain many files, and when you start to create fortran 2003/2008 code you will want to use “.f2003” or “.f03” instead of “.f90” . The Code::Blocks IDE is well suited for the former tasks, and we will return to these later. Playing with this IDE is the only way to learn about all its options. Two really nice plugins are “Format Fortran Indent” and “Code statistics”. The first one can be used to auto-indent your Fortran code, making it easier to find those nasty missing “end” statements. The code statistics tool runs through your entire project and tells you how many lines of code you have, and how many lines contain comments.