How to Compile and Run C/C++/Java Programs in Linux
Last Updated :
30 Sep, 2022
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");
}
}
Similar Reads
How To Compile And Run a C/C++ Code In Linux C Programming Language is mainly developed as a system programming language to write kernels or write an operating system. C++ Programming Language is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance. In this article, we will be compiling and exe
4 min read
How to Compile, Decompile and Run C# Code in Linux? C# is a modern multi-paradigm programming language developed by Microsoft and released in the year 2000. By multi-paradigm we mean that it includes static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented, and component-oriented programming discip
2 min read
How to Run a File in Linux The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether itâs a script, a compiled program, or even a text file. In this article,
6 min read
How to add "graphics.h" C/C++ library to gcc compiler in Linux While trying c graphic programming on Ubuntu, I figured out that graphic.h is not a standard C library and it is not supported by gcc compiler. So I am writing this article to explain the process.If you want to use graphics.h on Ubuntu platform you need to compile and install libgraph. It is the imp
2 min read
How to Install Java JDK9 on Linux? As we all know Java is widely used in tech industries, with around 4 billion devices currently using java. This Java installation is going to be very easy and beginner-friendly. In this article, we are going to see a step-by-step guide on how you can install java on your Linux machine. Step-By-Step
4 min read