Wednesday, July 3, 2024
HomeLanguagesPythonHow to Compile and Run C/C++/Java Programs in Linux

How to Compile and Run C/C++/Java Programs in Linux

C is a procedural programming language. It was initially developed by Dennis Ritchie between 1969 and 1973. It was mainly developed as a system programming language to write an operating system. The main features of C language include low-level access to memory, a simple set of keywords, and clean style, these features make C language suitable for system programmings like an operating system or compiler development.
First, we need to install some development tools and applications such as GNU, GCC, C/C++ compiler to compile and execute the code on Linux. You can verify the installed tools using the following command:

To check the version information of cc:

cc -v

Now consider a simple C program file named as Geeks.c as follows:




#include <stdio.h> 
int main(void
    printf("Hello! Geeks\n"); 
    return 0; 


To compile this code we can use:

cc filename.c -o executable_file_name

Here, filename.c is the C program file and -o option is used to show the errors in the code. If no error founds then this will generate an executable file as executable_file_name.

cc Geeks.c -o Lazyroaroutput

Here, Lazyroaroutput is the executable file which is generated. So we can execute it like:

./Lazyroaroutput

For C++ Program Files:

C++ is a general-purpose programming language and widely used nowadays for competitive programming. It has imperative, object-oriented and generic programming features. C++ runs on lots of platform like Windows, Linux, Unix, Mac etc. Before we start programming with C++. We will need an environment to be set-up on our local computer to compile and run our C++ programs successfully. You can verify the installed tools using the following command:

To check the version information of g++:

g++ --version

Now consider a simple C++ program file named as Lazyroar.cpp as follows:




#include<iostream>  
using namespace std; 
    
// main function
// where the execution
// of program begins 
int main() 
    // prints Hello World!
    cout<<"Hello World!\n"
        
    return 0; 


To compile this code we can use:

g++ filename.cpp -o executable_file_name

Here, filename.cpp is the C++ program file and -o option is used to show the errors in the code. If no error founds then this will generate an executable file as executable_file_name.

g++ Lazyroar.cpp -o Lazyroaroutput

Here, Lazyroaroutput is the executable file which is generated. So we can execute it like:

./Lazyroaroutput

For Java Program Files:

Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. You can verify the installed tools using the following command:

To check the version information of Java:

javac -version

To compile the code we can use:

javac filename.java

To execute we will use the name of the class which contains the main method as follows:

java classname

Example: Now consider a simple Java program file named as HelloWorld.java as follows:




class HelloWorld 
      
    // Main Method
    public static void main(String args[]) 
    
        System.out.println("Hello, GFG"); 
    


Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments