Pages - Menu

04 August 2011

C++ in Ubuntu (gedit)

Hi there,
      If you are a C++ programmer or you learn C++ in your school/college and you programmed in Borland C or Turbo C earlier in Windows and thinking it would be so good if I could write my programs in Ubuntu, here's a cookie for you. Unlike downloading compilers and debuggers in Windows, there is an easy way to write and test C++ codes in Ubuntu. This includes using the pre-included text editor (gedit) and two small scripts with some standard libraries which we would download.

Things we will need:
  • Gedit
  • Two small scripts (Compile and Run)
  • Ubuntu Build-essential package
1) Gedit
gedit is the official text editor of the GNOME desktop environment.
While aiming at simplicity and ease of use, gedit is a powerful general purpose text editor.
We can create and add external plugins to this text editor easily.

 2)  Compile and Run scripts
I have two scripts with me which are supposed to be added into gedit, you can download them from here,





3) Build Essential package
You will need Build Essentials for Ubuntu if you want to program anything, even if you download NetBeans IDE or any such IDE you will always need build-essential which contains all the libraries needed for programming

Download Buil-essential package by typing this in terminal
sudo apt-get install build-essential

Procedure

Do..

cd ~
mkdir -p  ~/.gnome2/gedit/tools
nautilus ~/.gnome2/gedit/tool
s

Copy and Paste the two downloaded scripts inside this newly created folder

chmod +x ~/.gnome2/gedit/tools/*


Now make a new C++ file in gedit by right clicking on desktop > Create Document > Empty File and name it as any_file_name.cpp . In gedit open Preferences (Edit > Preferences) go to Plugins tab and enable External Plugins.

After enabling External tools you would be able to see External Tools menu Tools > External Tools
Now write a simple C++ program and try executing it.
The scripts also add some shortcut keys for compilation and running the program
Press F5 to compile, now click "Shell Output" once and hit F7, this would run your program in a new Terminal window.


















Note: The programs you write are not exactly similar to the ones which you did in Turbo/Borland C++. You will have to make some changes..
Some of these changes include
1) Including .h extension to header file does not work try removing it and vice versa
for example: #include won't work, instead use #include
2) There is no conio.h header file to stop the screen from getting closed, you will have to use stdio.h and put getchar(); two times for pausing the screen
3) Always use "using namespace std; " after all your include files.

Always remember to save the file before compiling

Sample C++ program for Ubuntu














Please let me know if you have any problem installing or running the program :)

No comments:

Post a Comment