Showing posts with label Linux Programming. Show all posts
Showing posts with label Linux Programming. Show all posts

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

Friday, December 7, 2012

Automatic Voice Command Execution


Hello Readers, this is a new concept that I am going to explain you in this post. Before starting main topic I will like to clear basic concepts, because if you don’t know the basic concept then you will not understand this concept properly. And according to my knowledge still this concept is not developed, and this is only concept that I thought and if you want, you can develop it.
Now back to the basic concepts, you should know voice frequency, how to convert voice frequency into text, and programming on any Linux Operating System, and the most import Multithreading, Because if you don’t know multithreading then you can’t develop it.
As we know voice is nothing but the frequency and we can easily convert analog frequency (sin wave) into digital frequency (square wave) by using some methods and once it converted into digital frequency, we can easily convert it into text.
Fig : Automatic Voice Command Execution
Step 1 is completed, now in next step we have to check a file where all the commands are stored but it is a hard task to recognize actual command because the voice frequency which was converted into text that may be converted into wrong formats or may be spelling of commands converted in wrong way. But this problem will solve by proper converting, for this we have to check (debug) all possible conditions.
Now in 3rd and final step we have to read all commands one by one and simply execute them on terminal of Linux Operating System that you are using for operations. The last step is seen to very easy but it not, because we have to execute all commands one by one that is easy but when to execute or when to wait (if file is empty) that is so much difficult but there is a solution that is multithreading. For more information of multithreading you can refer to any book or you can search it on http://www.google.com. To execute all commands we require two or more threads, its totally depends on Programmers logic.
I am hoping that all readers will like this concept and if anyone wants any kind of help to develop this concept then you can mail me on panditdilipr@gmail.com.