
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.
- 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.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. - Install CB in your home directory, e.g. ~/codeblocks
cd /opt sudo mkdir /opt/codeblocks sudo mv ~/Download/CodeBlocks_Fortran_v1.2_Linux32/* /opt/codeblocksmkdir ~/codeblocks mv ~/Download/CodeBlocks_Fortran_v1.7_Linux64/* ~/codeblocks - 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.shExec=~/codeblocks/codeblocks_run.sh Terminal=false Type=Application Categories=Development Icon=~/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++)
„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.
It was very useful, thank u.
this article very interesting. With this sharing, i can setup codeblocks to run fortran.
Code::Blocks for Fortran moved from http://darmar.vgtu.lt to http://cbfortran.sourceforge.net/
Thanks very much. Just installed the 64-bit binary in /opt/cb, made a launcher, and it works as I am familiar with it in Windows. No need for xterm (I use the one with Xfce4), no need (apparently) for wxPython (I thought I saw that requirement somewhere, now cannot find it).
Also, since am using Slack 14.1, no need either for ‘build-essentials’ or gfortran. Your instructions are good enough.
Anyway, thanks very much – great job !
Addendum:
pkexec
fix added.Very useful post. I ended up here while fixing the broken link to the damar-page in my own tutorials for C::B-fortran installation on windows for my students:
http://dannyvanpoucke.be/fortran-tutorials-en/
Thank you for your comment and to link this article on your website, Danny.