Install and Setup Code::Blocks for Fortran on GNU/Linux
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.
- Download Code::Blocks for Fortran for the appropriate architecture (32-/64-bit) from the developer’s homepage
- 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.
- 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.2_Linux32.tar.bz2
- Install CB in e.g. /opt/codeblocks
cd /opt sudo mkdir /opt/codeblocks sudo mv ~/Download/CodeBlocks_Fortran_v1.2_Linux32/* /opt/codeblocks
- Create a shortcut for your app-launcher menu. Start your text editor as root and create a desktop file.
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 Terminal=false Type=Application Categories=Development Icon=/opt/codeblocks/share/codeblocks/images/codeblocks.png
- 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”.
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
- “File|New…|Project…”, choose “Fortran application”
- 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:
The other fields are filled out automatically.
- In the next screen check if everything is as you like and click finish:
- 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.
Links to install and run Code::Blocks on MS-Windows
- 10 minutes to setup Modern Fortran 2003 / 2008 with CB on Windows
- Install gfortran: Windows Binary Installation
- Code::Blocks Tutorial by Michael Doherty and modifications by William Ford (C/C++)