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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.