Showing posts with label gcc. Show all posts
Showing posts with label gcc. Show all posts

Thursday, February 7, 2013

जिज्ञासा


A Practical workshop on Fedora…
-->The SnashLUG Nashik Linux User Group had organized the third event on 2nd and 3rd Feb, 2013. This was a technical event name जिज्ञासा, -->
which was a practical workshop on Fedora. This event was conducted by Shakthi Kannan who is the ambassador of Fedora, also he is Senior Software Engineer, Fedora and also he is the moderator of Linux forum. The purpose of this event was to get knowledge about the basic things to start a project.
The contents of this workshop are as follows:-
<        
           I want to do project, tell me what to do?
<         Gcc
           Git
           Openstack
 On 2nd Feb, he gave a seminar on the topic “I want to do project, tell me what to do”. In that seminar, he gave us the basic knowledge about how to start a project and plan little things so that we can extend our project in a more flexible way. He told about how to make small targets and plan for those targets to achieve it. He also gave some ideas about how to deal with the corporate world and how to manage and plan things.

      
-->
Then he took a workshop on “git” and “gcc” on the same day. git is distributed version control system (dvcs) which is written in C language. A version control system allows the creation of a history for a collection of files and includes the functionality to revert the collection of files to another state which may be a different collection of files or contents in the files. A distributed version control system has no central server which stores the data. Every local copy contains the full history of the source code. He gave us detail information about the git how it actually works. Then he told about the gcc which is the “GNU” C Compiler that Linux used to run the C programs. He taught us about how the compiler exactly compiled the codes. Also he taught us about how to make our own library functions so that we can use it later on. 

Then on 3rd Feb, he took a workshop on OpenStack. Openstack is a Infrastructure as a service (IaaS) cloud computing project that is free open source software released under the terms of the Apache License. The technology consists of a series of interrelated projects that control large pools of processing, storage and networking resources throughout the datacenter, all managed through a dashboard that gives administrators control while empowering their user to provision resources through a web interface. He gave us the great knowledge about the OpenStack. 

-->

-->He also told us about the various links about the conferences and projects based so that we can search and can learn about the various topics. This event was very successful and again got a great response from all. We enjoyed a lot attending his workshop and got a great knowledge.

Saturday, December 22, 2012

Compile and execute C++ program on Linux



The Borland Turbo C++ compiler which works on Windows operating System, does not have all the capabilities of C++ programming. The core C++ compiler is present on Linux operating System. The gcc is bundled with the compiler of C++ too. It is basically GNU C/C++ Compiler Collection. You may use g++ compiler to compiler your C++ program file on Linux.

Let's see, how to do it.

1.      Open any text editor such as gedit, vi, vim, or emacs and types your program in it. Save the file with extension .cpp. It is required to identify that the file contains C++ code.
2.      You need to write this code in pure C++ format. Look at the program no.1, which is written for Turbo C++ compiler and program no. 2 is written for gcc/g++ compiler.

#include<iostream.h>
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
void main()
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 1: Written for Turbo C++ Compiler.


#include<iostream>      //Line-1
using namespace std;    //Line-2
class Add
{
public:
int calculate(int a, int b)
{
return a + b;
}
};
int main()              //Line-3
{
int x, y, z;
Add a;
cout<<"Enter two numbers: ";
cin>>x>>y;
z = a.calculate(x, y);
cout<<"\nAddition is: "<< z <<endl;
}
Program 2: Written for Linux g++ compiler.

3.      Look at the changes made in the second program, which is written for g++ compiler. In line number-1 #include<iostream> is written instead of #include<iostream.h>. All C++ related header files must be included in this fashion only. If C programming header files such as stdio.h are required then these are included as usual fashion such as #include<stdio.h>.
4.    The Line-2 is an extra line in program-2. It is pure C++ statement. In C++, namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name. The keyword 'using' is used to introduce a name from a namespace into the current declarative region. All the files in the C++ standard library declare all of its entities within the std namespace. That is why we have generally included the using namespace std; statement in all programs that used any entity defined in iostream.
5.      The robust compiler like g++ says that your main() function must return only 'int', it must not be void. It is compulsory to return the int from main().
6.      In order to compile the program use command g++ on the terminal:

g++ add.cpp             [Command-1]

7.      It will compile the file, check for the errors, prints on screen if any and generates an a.out output file. The a.out is default output file name of g++ compiler. You may change it by providing -o option to the command such as,

g++ add.cpp -o addition [Command-2]

8.      Now, this will generate output executable file by name 'addition'.
Now execute the file as,

./a.out                 [Command-1 output]
./addition              [Command-2 output]

9.      Your program will get executed and generate output.

Enter two numbers: 56 41
Addition is: 97

Sunday, September 23, 2012

DDD: Best Visual Debugger


Debugging is the most important activity in the software and program development. In order to remove or identify the logical errors in the program; debugging is done. Linux uses the utility of ‘gdb’ (GNU Debugger) to debug the programs. It has a strong instruction set but lacking in visual GUI debugging utility. 
DDD or Data Display Debugger is a visual debugger made available under GNU public license uses the gdb activities with GUI at foreground! Not only gdb but ddd has in-built command-line debuggers such as DBX, JDB, HP Wildebeest Debugger (WDB), XDB, the Perl debugger, the Bash debugger, the Python debugger, and the GNU Make debugger etc. So, by this visual debugger you can debug programs of C, C++, Java, Shell, Perl, Python etc.
Here I am giving a small tutorial to show how to use ddd to debug the C programs. The advanced tutorials will be given afterwards.
Let’s start… As I told ddd is available under GPL, so you can download the debugger using following command directly by debian version Linux.
sudo apt-get install ddd
or you may go to ‘Ubuntu Software Centre’ and search for ‘ddd’ as shown in the picture 1 below. You may get information about it and install from there.
[Note: If images are not visible, click on it to view large]
Picture 1: DDD entry in Ubuntu Software Centre
For example, you have written a C program code named ‘basic.c’. Compile it using gcc in the following fashion:
gcc –g –o basic basic.c
here, -o suggests the name of the output file that you will create after compilation of ‘basic.c’ and –g suggests that you are giving ability to debug this program. If the –g is missing your program can’t be debugged by gdb or ddd. After compilation you will get an executable file named ‘basic’ (without extension). Note: you may give any name to your executable file at the time of compilation after option –o.
Your program will be compiled successfully as shown in the picture 2 below.
Picture 2: Compilation of program for debugging
Now, open your ‘ddd’ by simply typing ‘ddd’ on the terminal.
Open your program ‘basic.’c by using ‘File’ menu -> open program option. Your program will be opened as shown in the picture 3 below.
Picture 3: The ddd windows
Your program will be visible in ‘Program Window’. Below of it, you may find ‘Assembly Code Window’, where the assembly equivalent code is shown. And below of this window, your gdb command line debugger is shown. You may instruct program directly by using gdb also! 
In order to set a breakpoint inside the program move your mouse on to respective line and press right click. You may get options as shown in the picture 4 below.
Picture 4 : Setting a breakpoint
The any number of breakpoints can be set in a single program. It is used for executing the program till that point. From that line onwards, you may execute program step-by-step. The breakpoints in the function can be set by typing the command ‘b main’ in the ‘gdb window’ also! Or ‘b 12’ can also be written. Here 12 is the line number in the program.
Now, go to ‘Program’ menu and click on the ‘Run’. Your program will get executed till the first breakpoint set. See the picture 5 below:
Picture 5: Watch variable values by F6
The green arrow shows the line which is in execution currently. Red dot shows the breakpoint. Now, from here onwards you may execute step-by-step just by pressing function key ‘F6’. The program execution will be processed step-by-step as you go on pressing ‘F6’. You may find arrow is traversing with the lines of execution. When you move the mouse on any variable used in the program, you may get the value of the variable on current execution. In the picture 5, you may see the values inside array ‘a’ is shown by the ‘ddd’ at mouse position. By this way you can check the contents of the variable at any point in the program.
You may check the content by typing the command in the ‘gdb window’ as shown in the picture 6 below. It has shown the contents of a[i] by typing ‘print a[i]’ in the gdb window.
Picture 6: Use gdb window.
The features of ‘ddd’ are not limited with this. It has a rich set of applications. Just find the options given in the ‘ddd’s menu. You will learn a lot!