A Computer programming is defined as the process of designing and building an executable program for accomplishing a specific computing task. There are different types of programs to do this. We will be seeing how to execute them in a Linux machine.
An interpreter is the one that helps to execute a program. Every program has an individual interpreter.
A program may be classified as,
1.Compiler Programs.
2.Direct Programs.
Compiler Programs - Programs that uses compilers before executing
C
C++
Java
Direct Programs - Programs that can be executed directly
Python
Perl
Ruby
Bash
As i said earlier in this article we will see how to execute different programs that will print "Testing Program for Linux!!!".
Compiler Programs:
C:
A C program has an extension .c, In C programs we need to compile the program before executing. For this we need gcc(GNU Compiler Collections) to be installed in our Linux machine.
Install gcc with the below commands,
For Debian machines:
$ sudo apt-get install build-essential
For RPM machines:
$ sudo yum install gcc
Check the version:
$ gcc –version
Create a file with your favorite text editor as myfile.c.
jhony@ljlinux:/home/programs$ vim myfile.c
Paste the below code inside the file.
Code:
#include <stdio.h>
int main()
{
printf("Testing Program for Linux!!!\n");
return 0;
}
jhony@ljlinux:/home/programs$ ls
myfile.c
Compiling:
Compile the program using gcc command.
jhony@ljlinux:/home/programs$ gcc myfile.c
jhony@ljlinux:/home/programs$ ls
a.out myfile.c
Now you will get an executable file as a.out
Executing:
You can either execute the file using ./ or calling the whole path of the file.
jhony@ljlinux:/home/programs$ ./a.out
Testing Program for Linux!!!
C++:
A C++ program has the file extension .cc, Same as C programs we need to compile the program before executing it.
Create a file with your favorite text editor as myfile.c.
jhony@ljlinux:/home/programs$ vim myfile.cc
Paste the code inside the file.
Code:
#include <iostream>
int main()
{
std::cout << "Testing Program for Linux!!!" << std::endl;
return 0;
}
jhony@ljlinux:/home/programs$ ls
myfile.cc
Compiling:
Compile the program using g++ command.
jhony@ljlinux:/home/programs$ g++ myfile.cc
You can also have a specify output file for your program by passing -o flag while compiling,
jhony@ljlinux:/home/programs$ ls
a.out myfile myfile.cc
Executing:
You can either execute the file using ./ or calling the whole file of the path.
jhony@ljlinux:/home/programs$ ./a.out
Testing Program for Linux!!!
jhony@ljlinux:/home/programs$ ./myfile
Testing Program for Linux!!!
Java:
Java files (.java) cannot be executed directly. Hence it also requires a compiler.
In Java the compiler's job is to translate Java files into "class files" which are binary coded that the virtual machine can execute. These virtual machines are termed as Java Virtual Machine(JVM) .
Create a file name as myfile.java
jhony@ljlinux:/home/programs$ vim myfile.java
Paste the below code inside the file.
Code:
class TestingProgram
{
public static void main(String[] args)
{
// prints Testing Program for Linux!!! on console
System.out.println("Testing Program for Linux!!!");
}
}
Compiling the Program:
The java compiler javac creates a .class file with a prefix of your main class. The .class file contains only binary code of your program. As said earlier, the Java byte-code is the intermediate representation of Testing Program.java program that contains instructions the Java interpreter will execute.
jhony@ljlinux:/home/programs$ javac myfile.java
jhony@ljlinux:/home/programs$ ls
myfile.java TestingProgram.class
Note: The Java compiler (javac) can compile multiple .java files together.
Or you can use a wild card operator * to compile all files
jhony@ljlinux:/home/programs$ javac *.java
Executing the Program:
After compilation of Your_Program.java to Your_Program.class, we use the Java interpreter to execute it which looks for the main class. Hence you will be using your class name for execution not your file name.
jhony@ljlinux:/home/programs$ java TestingProgram
Testing Program for Linux!!!
Comments