Install and Setup Code::Blocks for Fortran on GNU/Linux

Updated on Nov. 7th, 2020

A new IDE for Fortran is in town. Beneath Photran the IDE Code::Blocks is available. Since Photran is specialised for Fortran, CodeBlocks is made for C/C++ programmers in the first place. But Darius Markauskas developed a Fortran plugin and provides a special Fortran version of CodeBlocks.

This How-To is for GNU/Linux and Fortran novices and shows how to install CodeBlocks for Fortran system-wide on GNU/Linux (here on Mint), compile your first program and get it run.

Like Photran CodeBlocks (CB) is cross-platform. But the binary Fortran version is available for Windows and GNU/Linux only. For Mac OS X you need the original CB and the plugin “Fortran Project” which you have to compile. The wiki shows you how. To get CB running on Windows refer links at the end of this article.

The installation on GNU/Linux is quite easy but there are some issues novices may struggle. It took some time for me till I noticed that CB needs xterm to execute console apps by default and it was not installed on my system.

Installation and setup

Code:Blocks is available in the repositories of Ubuntu and Mint and maybe in many other distros. But the Fortran plugin is absent. I recommend to use the binaries of the complete Fortran version provided on the Plugin-Homepage.

  1. Download Code::Blocks for Fortran for the appropriate architecture (32-/64-bit) from the developer’s homepage
  2. Install the packages xterm, build-essential and gfortran with your GUI package manager or via terminal. On Debian-based systems:
    sudo apt-get install build-essential gfortran xterm

    CB uses xterm to execute console applications. If you want to use your standard terminal, go to “Settings|Environment|General settings” to edit it after you finished the installation. In this case you can skip xterm installation.

  3. Unpack the CodeBlocks archive (here the 32-bit version) with your GUI archive app or in the terminal:
    cd ~/Download
    tar xvjf CodeBlocks_Fortran_v1.7_Linux64.tar.bz2

    Addendum 2020-11-07:
    Code::Blocks does not work properly, when installed as root or located in a directory with root write-access only. You need write access. The best way to install it is to copy it in your home directory, as described below and not in /opt as suggested in an earlier edition of this article.

  4. Install CB in your home directory, e.g. ~/codeblocks
    cd /opt
    sudo mkdir /opt/codeblocks
    sudo mv ~/Download/CodeBlocks_Fortran_v1.2_Linux32/* /opt/codeblocks
    
    mkdir ~/codeblocks 
    mv ~/Download/CodeBlocks_Fortran_v1.7_Linux64/* ~/codeblocks
  5. Create a shortcut for your app-launcher menu. Start your text editor as root and create a desktop file.

    Addendum 2016-07-31:
    Since Trusty Tahr (version 14.04) Ubuntu does not have “gksu” installed by default to open graphical applications as root. On Xenial it is possible to install “gksu” manually but in future versions it  will not be provided anymore maybe. The successor is the command “pkexec”.

    Unfortunately “pkexec” needs PolicyKit files for every graphical application it wants to start. There are no PolicyKit-files for Gedit and Nautilus installed by default. Because we need Gedit as root later we catch up this now by typing two commands in the terminal:

    wget https://raw.githubusercontent.com/hotice/webupd8/master/org.gnome.gedit.policy -O /tmp/org.gnome.gedit.policy
    sudo cp /tmp/org.gnome.gedit.policy /usr/share/polkit-1/actions/

    Refer this webupd8.org-article for details in this matter.

    pkexec gedit /usr/share/applications/CodeBlocks.desktop

    or on KDE use kdesudo kate instead of pkexec gedit and fill in the following content and save:

    #!/usr/bin/env xdg-open
    [Desktop Entry]
    Encoding=UTF-8
    Name=Code::Blocks
    Comment=IDE for C/C++ and Fortran
    Exec=/opt/codeblocks/codeblocks_run.sh
    Exec=~/codeblocks/codeblocks_run.sh
    Terminal=false
    Type=Application
    Categories=Development
    Icon=~/codeblocks/share/codeblocks/images/codeblocks.png
  6. Now open CB the first time (you find it in the menu category “Development”) and choose “GNU Fortran Compiler” and click on “Set as default” then “Ok”.
    CodeBlocks - Default compiler

    Code::Blocks – Default compiler

    FYI: You can edit this later in “Settings|Compiler” if you skip this dialog by mistake. You are done.

Compile and run a Fortran program

  1. “File|New…|Project…”, choose “Fortran application”

    Code::Blocks - New project

    Code::Blocks – New project

  2. Skip the next screen and in the screen after give the project a title “HelloWorld” and a folder where you want to create the project in:
    Code::Blocks - Skip info

    Code::Blocks – Skip info

    Code::Blocks - Specify project

    Code::Blocks – Specify project

    The other fields are filled out automatically.

  3. In the next screen check if everything is as you like and click finish:

    Code::Blocks - Check targets

    Code::Blocks – Check targets

  4. A project with a HelloWorld program is created automatically. “Build|Build and run” or F9 starts the compilation and executes the app after success in xterm.
    Code::Blocks - First project finished

    Code::Blocks – First project finished

    Code::Blocks - First project output

    Code::Blocks – First project output

Links to install and run Code::Blocks on MS-Windows

Picture Credits

Tux“ by Larry Ewing - image source - drawing description. About Wikimedia Commons.

"Punch card Fortran Uni Stuttgart (6)" by Harke - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons.


7 comments

Leave a Reply to Hani Cancel reply

Your email address will not be published. Required fields are marked *