Tag Archives: gcc

compiling-executing-c-program

How to Compile ‘n’ Execute/Run C/C++ program in Ubuntu 12.04

Now a Days, Ubuntu is one of the most popular OS among programmers (not only nerds but also the beginners who just entered into the programming world) but for a beginner Ubuntu user coming from Windows may feel some problem in programming with Ubuntu.

On Windows they use nice GUI based IDE, but in Ubuntu things are little different. In Ubuntu it’s better to use Terminal (specially for the beginners), instead of GUI based IDEs such as Eclipse, Netbeans etc for compiling programs. In fact, the command line approach is much easier and efficient, due to the powerful shell such as bash, zsh etc.

So, in this post – you will learn – how to compile and execute (run) C/C++ programs in Ubuntu 12.04 (Precise Pangolin)/12.10(Quantal Quetzal) or other Linux distributions such as Linux Mint 13 (Maya). You don’t need to install any extra applications or tools other than the compiler. The default text editor – gedit will work fine for source code editing (unless you have a preferred source editor such as Emacs, Vim, Nano, Kate or something else).

#1. Install the C/C++ Compiler

First make sure that you have GCC (GNU Compiler Collection, for C language) and G++ (for C++ language) installed on your system. If not, then install it by typing the following commands on Terminal –

For C

sudo apt-get install gcc

For C++

sudo apt-get install g++

#2. Write the Program (Source Code)

Open gedit and write the following lines of code (it’s a simple program that contains one print line and one comment, just for explanation purpose) –

#include<stdio.h>

void main()
{
	printf("Hello! Human!\n");
	/* Do something more if you want */
}

Then save the file as hello_human.c on your Home Directory (~). If you ae writing a c++ program, then give it the extension as .cpp (and of course you will also have to change the command accordingly during compilation)

#3. Compile it

The command structure is :

gcc source_file_name.c -o executable_file_name

if leave the -o option (name of the output file) then by default a.out will be created as the executable output file.

To execute the above example program, open a terminal and type –

cd ~
gcc hello_human.c -o hello_human

[In case of C++, just replace gcc with g++, rest of the things are same, e.g g++ source_file.cpp -o executable_file]

#4. Execute It

On Terminal, type (from the same directory where you have the executable file, in this case, it’s Home Directory(~) ) –

First make the script executable (sometimes, it may not be necessary)

chmod +x hello_human

Then run the program using the command –

./hello_human

Now you should get the output –

Hello! Human!
c programming in ubuntu 11.10

How to Compile and Run C/C++ program on Ubuntu 11.10

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 are available free of cost. Some of them are  – GCC – The greatest compiler for C language (from FSF (‘Free Software Foundation’ by Stallman); Linux Torwalds used GCC while developing Linux Kernel), Eclipse IDE (The most popular ‘Integrated Development Environment’ for Java programmers), Netbeans, KDevelop, Codelite  etc.

C/C++ language is a high level programming language (although the term high and low is used in relative sense e.g C is a high level language as compared to Assembly but if we compare it with java then C is a low level programming language; the term high or low basically describes the closeness with hardware). Most of the operating systems has been written in C language. This post has been written for beginners who just started learning C/C++ or the programmers who have migrated from Windows to Ubuntu (although the commands are almost same for all Linux based operating system).

Compiling and Executing C program in Ubuntu 11.10

1. Write and save the program

c programming in ubuntu 11.10

Open a simple text editor (e.g gedit), IDE (Eclipse) or command line code editor (Nano or Vim). I’ll be using gedit as it is very simple to use and it’s recommended for beginner programmers. Right Click on Desktop or any directory (Browse File using Nautilus) and select create new File – hello.c (.c extension is used to indicate that it’s a c program). Then write a simple program like this (and save the program press Ctrl+S)

#include<stdio.h>

void main()
{
	printf("Hello! This is my first C program with Ubuntu 11.10\n");
	/* Do something more if you want */
}

2. Compile the program

GCC (GNU Compiler Collection) is installed by default, in Ubuntu. To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)

gcc hello.c -o hello1

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.

3. Execute the program

To execute the program, you need to run –

./hello1

running a C program in Ubuntu 11.10

Compiling and Executing C++ program

The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command –

sudo apt-get install g++

If you have any problems then share it through comments. One more thing, This video might help you in running your first C program in Ubuntu 11.10(I’ve recorded it in Gnome Shell interface) –

compiling-executing-c-program

how to compile and run C/C++ program on ubuntu 11.04

C/C++ is one of the most popular programming language, mostly used in system programming (system software development e.g Most of the modern operating systems are programmed (primarily) either in C language or C++). C is a High Level language as compared to Assembly program, so it comes in the middle of Assembly and Java like High Level Language… That’s the beauty of C – it’s very easy to write program in C as compared to Assembly programming and fast execution time as compared to Java.

By default, Ubuntu 11.04 (code name:=Natty Narwhal) or any other Linux Based Distribution like Fedora, Linux Mint(10 or 9), Debian etc.. are very good for programming purposes(e.g C, C++, Java, PHP, Perl, Scripting, Ruby etc.). A lot of free and open source tools like IDE, debuggers are available which will makes the programming a lot Funky, with Ubuntu. In this post you will learn how to compile a C/C++ program on Ubuntu 11.04, from command line, you can also use some IDE like Eclipse, NetBeans if you want (but it’s not recommended for beginners). Ubuntu 11.04 comes with gcc(GNU Compiler Collection), installed by default which is one of the most widely used modern C compiler.

Compiling and Running C program on Ubuntu 11.04

#1 : Write a program, better use gedit (The Default Text editor). Right click on Desktop and create a new file – ‘fun.c’ and copy the code, given below and save it (CTRL+S). (C programs have the extension .c while C++ programs, have .cpp, although it’s a different fact that Linux doesn’t recognize file based on their extension but some applications may do that and it also avoid confusion, so better use proper extension)

#include<stdio.h>
void main()
{
/* Don't take it seriously, it's just for Fun! */
printf("Have Fun!\n");
}

#2 : Now you compile the program using GCC compiler (I think it’s installed by default in most versions in Ubuntu, but if it’s not the case then install it by typing – sudo apt-get install gcc at terminal). Open the Terminal (CTRL+ALT+T) and type the command (First move on to the directory where your file is located, I assume you have created the file on Desktop, If you are an absolute beginner – Learn Some basic Linux Commands).

cd Desktop
gcc fun.c -o fun1

The -o option (specifies the Output File Name) in the following command is optional, but it’s a good practice, because if you won’t specify that – then a default a.out file will be created (which will eventually overwrite older a.out file in that directory).

#3 : Execute/Run the program. Type  –

./fun1

#4 : That’s all. Here is one snapshot of output terminal –

compiling-executing-c-programCompile and Run C++ program

To compile C++ program, you may need to install g++.

Installing g++ compiler on Ubuntu 11.04

sudo apt-get install g++

Then all the remaining procedure is almost same. Just replace gcc by g++ from the above tutorial and instead of a basic/simple c program, use C++ program.

g++ file_name.cpp -o fun2
./fun2
Programming with gcc

GCC-GNU Compiler Collection version 4.6.0 is out!

GCC(GNU Compiler Collection, version 4.6 was released a day ago) is a compiler available free under GPL by GNU Foundation. it is cross platform and is written using c/c++. Actually GCC has a major role in the growth of open source/Free Software world, in the past few years.

A majority of C/C++ programmers uses GCC, on Linux based operating systems as well as on Windows/Mac OS X. Since I uses Ubuntu 10.10 as my primary desktop; I know how important ,the gcc is, for programming Fun. GCC is mainly used as a compiler for C,C++,java,Fortran,Pascal,Ada,Objective-C.

A lot of extra features can be added with help of freely available plugins. GCC is one of the best compiler for c programmers.

Programming with gcc

New Features in GCC 4.6

  • Support for GO Programming Language has been added
  • New Optimizer Levels(-Ofast) has been introduced
  • Improvements in compiler time and Memory Usage
  • other platform specific improvements

GCC vs 4.6.0 has released(on March 25,2011). Go here for Complete details on the features of GCC.