Saturday 20 September 2008

Compiling C/C++ Code in Ubuntu and Available IDEs

One of the many questions users who switch from Windows to Linux have is 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.

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.:

#include <stdio.h>

int main ()
{
printf ("Hello, world!\n");
return 0;
}


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.

Kate
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.
Homepage

sudo apt-get install kate

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

sudo apt-get install gedit

Geany
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.
Homepage

sudo apt-get install geany

KDevelop
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.
Homepage

sudo apt-get install kdevelop

Code::Blocks
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.
Homepage

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:

./configure
make
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.


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

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.
Homepage

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.
Homepage

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):
Homepage

sudo dpkg -i codelite_1.0.2785-ubuntu0_i386.deb

Emacs and Vim
Both Emacs and Vim became legends on Linux and they both have fans who continuously argue on which one's better. I personally prefer Emacs over Vim, but I recommend you to try both and see which one fits you better. Emacs is not only an IDE, it's also an e-mail client, eventually IRC client, file browser and more. Regarding speed, Emacs is way slower than Vim, and as IDEs, both include great features. Both Emacs and Vim use their own concepts and keyboard shortcuts, so as a beginner you'll (probably) find them a little hard to learn, but this doesn't mean you don't have to use them. Put some effort into learning at least one of them and you'll see in time how helpful this learning process is. Another note: most systems (including web servers which you'll usually ssh to in order to manage your web page - if you have one) include Vim by default, but not Nano or Emacs.
Emacs homepage
Vim homepage

sudo apt-get install emacs
sudo apt-get install vim

Update: As I already mentioned earlier in this article, Dev-C++ is not available on Linux, however if you really, really want to work in it instead of a native IDE, you can successfully run it through Wine. To install Wine either type sudo apt-get install wine (which will install 1.0) or to install the latest release use one of the following tutorials (they all work for Ubuntu 8.10, 9.04 Jaunty Jackalope and the latest Wine release): tutorial 1, tutorial 2, tutorial 3. After setting up Wine download Dev-C++ from here, and run wine devcpp-4.9.9.2_setup.exe to install it. Refer to this page to see what additional libraries you will need for running binaries compiled with Dev-C++. The executables will be located by default inside the ~/.wine/drive_c/Dev-Cpp/Packages directory, where ~ is your home directory.


Have some other questions regarding compiling or using IDEs on Linux? Or maybe a correction or suggestion regarding this tutorial? Please feel free to discuss in the comments below.

Updated: Aug 13, 2011

40 comments:

Anonymous said...

I think Eclipse deserves to be mentioned here.

Craciun Dan said...

Thanks for mentioning it, I completely forgot about Eclipse. I think I'll make an addition and include it too, since it really deserves mentioning and it's a complex application.

Anonymous said...

Eclipse

sudo apt-get install eclipse-cdt

Just make sure you are using Sun's JVM.

[]s

Popa Adrian Marius said...

I usually use eclipse for php/c++ related development

What i hate is that is an memory/cpu hog
but is very nice for coding php/jscript
http://mapopa.blogspot.com/2008/09/php-granymede-ganymede-packages-based.html
and i still have to figure out how to debug large c++ projects like firebird/flamerobin

Another thing on my to do : get egit working and to change the colors for code editor to make them more darker
like the vim in the console

Anonymous said...

I started with Eclipse since that is what I used on Windows. But later I found KDevelop which currently rocks my C/C++ programming world. KDevelop is right at the sweet spot for me. Quite a bit lighter and faster than Eclipse and has almost all the features. Currently I'm also starting to learn Emacs.

[NpoWEr] said...

No forgetting Eclipse!

Anonymous said...

Yeah, I think netbeans deserves mentioning as well, it's easily one of the most advanced IDE's for almost every language. netbeans 6.5 beta makes eclipse look like a text editor.

Anonymous said...

I tried using Anjuta for some c++ coding but I was never able to get it to work correctly. I'm definitely going to try some of these other compilers out.

Anonymous said...

Yeah, I think netbeans deserves mentioning as well, it's easily one of the most advanced IDE's for almost every language. netbeans 6.5 beta makes eclipse look like a text editor.

Agreed,

I would like to point out that both Eclipse and Netbeans are both originally designed to create JAVA programs but are both very capable of doing C/C++ with the correct plugins. Netbeans does know it's C/C++ very well.

Anonymous said...

Boa is full fledge RAD for crossplatform Python and wxPython apps:

apt-get boa-constructor

Priyank Bolia said...

CodeLite C++ IDE for Ubuntu is the best I have used so far.

Anonymous said...

how about Netbeans 6.5

Anonymous said...

Don't forget Xemacs!!!

Anonymous said...

Re: about NetBeans 6.5

It's still in beta. 6.1 has everything necessary to have fun: Java, C++, Ruby. 6.5 will add PHP. To my taste, NetBeand does jRuby better than Eclipse.

However, after reading the post, my first thought was "Eclipse is missing". When I recall what Sun is doing, I get a sad feeling about Sun and NetBeans. Ther deserve better ...

GregD_Amherst said...

I tried using the command to execute "myapp" as outlined in the article but it didn't work. I get the following response:
bash: ubuntu@ubuntu:~$: command not found

Does the "bash:" mean that I'm using the bourne shell?

Should I be using another shell?

I use to be C programmer using an SGI version of UNIX. Once I compiled a program I was able to execute it from a command line just by entering the name of the executable - is this possible with UBUNTU?

Unknown said...

greg,

put a ./ in front of the command name.

> ./myapp

Most likely your unix environment had . included in the path. You can see you path by typing:

> echo $PATH

This is the location where executables will be searched for, and in the order they are listed.

If a dot is included in the path this will search the current working directory as well. This is generally considered a bad thing, but on your own machine its not just a big deal...

-tom

GregD_Amherst said...

Tom,

Thanks - your suggestion worked.

Robert said...

It might interest some people to know that nano can also do syntax highlighting. If it's not enabled by default in your distro, just put a copy of the nanorc file (usually can be found in /etc/nanorc, or google it) into your home directory as .nanorc. You can edit that file (nano .nanorc) and customize nano's behaviour as you like it (set tab widths, enable syntax highlighting, et cetera.)

Prassyy said...

I installed Anjuta via Add/Remove. Then after checking in couple of add-ons I finally made the Build Option come to live.
But I simply couldnt make the drop down options to work, as they were disabled.
This is first time with linux, so would appreciate any help.

Thank you.

PS: Very nice article.

Abelardo Jara-Berrocal said...

Hi, you do not need to compile Codeblocks in Hardy, it is already a repository for it

sudo gedit /etc/apt/sources.list

Then add this lines:

deb http://lgp203.free.fr/ubuntu/ hardy universe

deb http://apt.wxwidgets.org/ hardy-wx main

For the PGP:

wget -q http://lgp203.free.fr/public.key -O- | sudo apt-key add -

wget -q http://apt.wxwidgets.org/key.asc -O- | sudo apt-key add -

Finally:

sudo apt-get install libwxgtk2.8-0 libwxgtk2.8-dev wx2.8-headers wx-common

sudo apt-get install libcodeblocks0 codeblocks libwxsmithlib0 codeblocks-contrib

That's it, Code::Blocks ready to go, actually for me Code::Blocks is the best.

Netbeans is so slowwwwww.

KDevelop is just for KDE.

Anjuta is so incomplete.

Eclipse for C++ also very incomplete.

Code::Blocks also accepts you to use any compiler (this is also in Windows) which means that you can just set to use Intel C++ compiler, or tne standard GCC.

Anonymous said...

nano editor

Neelesh Jain said...

hi..thank you so much ...
for a 3-4 days i've been searching for an article like this...(i am new to linux dont know about it much)...
but this has helped me understand...
i am going to try code::blocks and ill surely comment on its use to a beginner like me>>>
thanks once again

Craciun Dan said...

You're welcome Neelesh :-) Hope Linux will work OK for you.

Anonymous said...

I have tried numerous IDEs in linux, but I think I have finally settled on CodeLite. Code::Clocks is pretty good, but CodeLite just seems a lot more polished.

http://codelite.org/

Craciun Dan said...

Thank you all for sharing these too. I updated the post accordingly.

Anonymous said...

Your instructions for building codeblocks on Ubuntu 8.04 fails when I try to run codeblocks: error while loading shared libraries: libcodeblocks.so.0: cannot open shared object file: No such file or directory

The link libcodeblocks.so.0 exists in /usr/local/lib so either its wrong or a ldconfg step got left out somewhere.

Craciun Dan said...

Are you sure you did everything right? Did you run sudo ldconfig after the installation? I can't think of anything now... it worked for me.

Anonymous said...

That was my point.

sudo ldconfig was left off your instructions for building codeblocks.

It gives the error message I reported when you follow your recipe.

sudo ldconfig is the final step which you left out of your instructions.

Actually, IMHO it should be in the make install part of the makefile which is already being run by root.

Craciun Dan said...

OK, I included it in the article now. Thanks :)

Unknown said...

I recall finding a port of Dev C++ to Linux on FreshMeat.

Popa Adrian Marius said...

now we have another option in jaunty qtcreator with qt 4.5

$sudo apt-get install qt-creator

http://thelins.se/learnqt/?p=5http://www.qtsoftware.com/products/developer-tools

Anonymous said...

Hi TuxArena,

Thanks for the blog post.
Good read. I'm just migrating over to Linux (Ubuntu) from a long time of developing on Windows under Visual Studio 6.0 up to VS 2008.
The review of the various IDE's was a lot of help.

-B.

Ciprian Khlud said...

You also forgot the KDevelop and/or QtDevelop and for persons that don't hate Mono, Monodevelop. All are integrated IDEs that do most work to you.

brownman said...

recompiling open source game

http://tronprog.blogspot.com/2009/06/tutorial-using-autotools-with-eclipse.html

vinay said...

thank you so much ,you solved my problems
thanks.

masheekasalam said...

thank dear tuxarena i was at a stage of losg my lap,, as i always used c and c++ in windows,, but my lap hav ubuntu..u helpd me to use c and c++ in my ubuntu.......thanks againn

abhilash said...

Ubuntu supports almost all of the IDE supported by most of the unix and linux systems such as Qt Sreator, Code::Blocks, Eclipde, MonoDevelop,Anjuta, Gambas, NetBeans and many more...IDE Ubuntu Ubuntu Development Environment

Anonymous said...

hey i m on the idconfig part and when i press enter after writing "sudo ldconfig"...it says nothing...!!!
now when i run codeblocks...its not able to comppile c++ programs....cant identify "conio.h"...!!!
Help me out of this !!!

Tomaz said...

KDevelop rocks

Techbee web Solution said...

Netbeans, the best IDE i have ever seen for compiling and running c/c++.. :) C Tutorial For Beginners