Open In App

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

Last Updated : 30 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report
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: C
#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 geeksoutput
Here, geeksoutput is the executable file which is generated. So we can execute it like:
./geeksoutput
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 geeks.cpp as follows: CPP
#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++ geeks.cpp -o geeksoutput
Here, geeksoutput is the executable file which is generated. So we can execute it like:
./geeksoutput
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: Java
class HelloWorld 
{ 
    
    // Main Method
    public static void main(String args[]) 
    { 
        System.out.println("Hello, GFG"); 
    } 
} 

Next Article
Article Tags :

Similar Reads