How to Compile C++ in Ubuntu

Users who switch from Windows to Linux have often ask, how to compile C/C++ sources and what IDEs (Integrated Development Environment) Linux has to offer?

Most of them study C or C++ at school or home and are usually used from Windows with an IDE like Dev-C++ or Code::Blocks.

In this article I’ll give a few explanations on how to compile software for studying purposes on Linux (and particularly Ubuntu), what are the most common ways, what I consider to be the most effective method and which are the most popular applications to use for programming in those languages.

Top Tip : Make sure you have a high level anti virus software like Kaspersky installed on your pcs and mac computers to prevent any nasty surprises

I’ll divert a little to say that Dev-C++, although a wonderful IDE on Windows, is no longer maintained, and even though a port used to be around for Linux, it was abandoned too (as far as I know). Instead, for those who would like a replacement which works and behaves the same way, I can warmly recommend Code::Blocks, which has an actively maintained port and it’s easy to compile and install. According to the details I could find on #ubuntu @ Freenode, Code::Blocks will also be included in the Intrepid Ibex (the next Ubuntu release) repositories, in universe.

Back to our topic. I think the simplest way to start with C/C++ in Ubuntu is to use first an editor like Nano and create a source file, then compile it using gcc (GNU Compiler Collection) in command line. But first, to install the GNU compiler and several other utilities for compiling sources, use:

sudo apt-get install build-essential

build-essential is a meta package – a package which only depends on other packages, so installing it will automatically install several tools like gcc, g++ and make.

Next, create your source file using a text editor of choice (I used Nano for this example):

nano main.c

Enter the content, e.g.:

1 #include</p>
2 int main ()
3 {
4 printf ("Hello, world!\n");
5 return 0;
6 }

Notice that I also included a newline after the close bracket, otherwise the compiler will issue a warning. Save it with CTRL+O, then exit Nano using CTRL+X. To compile your source, simply use:

gcc main.c -o myapp

The output, myapp, will automatically be executable, so to run it use:

ubuntu@ubuntu:~$ ./myapp
Hello, world!

This is the simplest way of creating and compiling C or C++ code.

Regarding more complex, powerful IDEs, you can try Vim, Emacs (which can be run both in CLI mode using emacs –no-window and in GUI mode) or even the user-friendly Nano. Nano can be configured by editing (or creating if it does not exist yet) the ~/.nanorc file, where ~ is your home directory. The global configuration file is located in /etc/nanorc. Also, you can read this tutorial on how to enable syntax highlighting in Nano.

Among the good editors which use a graphical interface are Kate, Gedit, Geany, KDevelop, Anjuta, Code::Blocks or Eclipse. These are not all though, but I recommend trying those first and see which one fits. I’ll briefly review some of them below, so you can have a general idea about each of them.

Its name means KDE Advanced Text Editor, but Kate is definitely not only a text editor. It supports highlighting in many languages, indentation, spell-checker, block selection mode, and it’s highly configurable. Kate comes by default in Kubuntu or can be installed using sudo apt-get install kate.

sudo apt-get install kate

This is the default text editor in GNOME. It can be used as a simple IDE too. It comes installed by default in Ubuntu.

sudo apt-get install gedit

Yet another editor written in GTK. It’s pretty light and includes the most common features an IDE should have, so it’s a good alternative to Gedit.

sudo apt-get install geany

This is the KDE advanced IDE, offering the tools and advanced features of a full IDE. I recommend starting with a text editor rather than using this one for studying purposes. However, if you especially want to develop KDE applications, KDevelop is the way to go.

sudo apt-get install kdevelop

This is the powerful port of Code::Blocks for Windows, using the wxWidgets interface. In my opinion it’s very fit for studying C/C++ on Linux. Although not included in Hardy Heron, Code::Blocks will be included in the Intrepid Ibex repositories.

Update: Ubuntu 9.04 comes with Code::Blocks included in the repositories, so you can install it using the usual sudo apt-get install codeblocks command. Ubuntu 8.10 and 8.04 users can follow the instructions below:

Notice: It is also a good idea to install xterm (a terminal application just like GNOME Terminal or Konsole), since Code::Blocks uses it to show the output of your programs.

sudo apt-get install xterm

1. Install the dependencies and compiler tools

sudo apt-get install build-essential
sudo apt-get install libwxgtk2.8-dev wx-common libgtk2.0-dev zip

2. Download the source code
Get the source from the official website, here, next uncompress it using:

tar -xjf codeblocks-8.02-src.tar.bz2

3. Compile it
Change the working directory to codeblocks-8.02-src and issue as usual:

1 ./configure
2 make
3 sudo make install

Finally, run ldconfig as root:

sudo ldconfig

This should do it. You can run Code::Blocks by typing codeblocks in a terminal or pressing ALT+F2 and writing codeblocks in the run dialogue that appears.

Written in GTK, Anjuta is a powerful development environment for C and C++, which also allows you to create GNOME applications.

sudo apt-get install anjuta

Addition: NetBeans
NetBeans is an advanced IDE written in Java from Sun Microsystems, and can be used for developing C/C++ code too.

sudo apt-get install netbeans-ide

Addition: Eclipse CDT
The package eclipse-cdt provides the Eclipse IDE with C/C++ development plugins. I found it slower than the others IDEs mentioned here, especially the interface.

sudo apt-get install eclipse-cdt

Addition: CodeLite
CodeLite is an open-source IDE similar with Code::Blocks. To install it, download the latest Ubuntu build (DEB package) from the official website, make sure the current working directory is the one where you saved it, and type the following command (replacing the filename with the latest name):

Leave a Reply

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