Ubuntu 11.10 (Oneiric Ocelot) is one of the most popular operating system for programming because there are lot of great open source applications, tools, compilers, debuggers, IDEs which are available free of cost.
One of these great compilers ia – GCC – The greatest compiler for C language from Free Software Foundation(FSF) by Stallman. Most of the operating systems including Linux kernel by Torwalds was written in C language
C and C++ language is a low and level programming language respectively ( the term high or low basically describes the closeness with hardware).
This post has been written for beginners who just started learning C/C++ or the programmers who have migrated from Windows to Ubuntu (the same for other linux distributions)
By default, every ubuntu distribution such as Ubuntu 11.10 comes with gcc compiler already installed. However, if one is not sure about this, there could be another installation. There are basically two ways of installing C/C++ compiler on ubuntu 11.10.
- The first approach is to use the “build-essential“. The “build-essential” contains a list of packages which are essential for building Ubuntu packages including gcc compiler, make and other required tools.To install using this approach, the following steps should be entered in the terminal.
sudo apt-get install build-essential
- The other approach is to install gcc and g++ compiler for C and C++ separately. To install C compiler, open the terminal and enter the command below.
sudo apt-get install gcc
After a successful installation, you can check the version of your compiler by entering this command in the terminal. .
sudo gcc -v
Compilation and Execution of C /C++ Program
- Write a C/C++ program
Open a simple text editor (in this case gedit) type the following codes and save as hello.c (.c extension is used to indicate that it’s a c program). Or write a program and save it as hello.cpp( .cpp extension is used to indicate that the program is c++ program)
- To compile the program, open the terminal and move to the target directory type the command for c program:
gcc hello.c -o hello
For C++ program enter this in the terminal.
g++ hello.cpp -o hello
- If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.