How To Compile And Run C Or C++ Program In Linux/Unix Terminal


Programming in Linux has various advantages over other operating systems. It provides a vast repository of open source tools required for the development of software as it is open source that means you can change every line of code as per your requirement and use.

Basically, Linux comes with pre-installed standard compilers and interpreters so you need not download it. In this article, we will discuss how can you use these tools to compile and run a c or c++ program in Linux or Unix system.

Compile And Run C Code in Linux

Linux/Unix provides a c compiler called gcc to compile and run the c code. Now follow the below steps –

1. Write your program in c

You can use any text editor to write your code. Here we will use the nano text editor in the command-line user interface to write our program. Type the following command in the terminal and then press enter.

nano <file_name_with_extension>

For example –

nano hello.c

use nano to create write c program

Write your code inside this file and then press ctrl+s to save the file and ctrl+x to exit from the editor.

We also have created a helloworld program in c which you can take a look if you are starting to learn C programming.

If you are using any other GUI editor once your code is complete save it with .c extension in your home directory. Or if you saved it somewhere else provide the full path of that file while performing any operations on it.

write the c program to compile

Now the file is saved into the home directory with the name hello.c. Its time to compile and run our code.

2. Compile and run code using gcc in Linux Terminal

Type the following command and press enter to compile the code –

gcc hello.c -o hello

And then execute the following command to run code –

./hello

compile the c program in a terminal in linux

Compile And Run C++ Code

Running c++ code is similar to c  but we will have to make few changes. First one is to use .cpp extension instead of using .c for c++ files and g++ compiler in place of gcc compiler.

Now follow the steps that are given below –

1. Write your program in c++

Type nano <file_name_with_extension>in the terminal and then press enter.

For example –

nano cpp_hello.cpp

Write your code inside the file which is opened in the terminal. Press ctrl+s to save the file and then ctrl+x to exit from the editor. Again if you are using a GUI based text editor once your code is complete save it with .cpp extension in your home directory.

c++ program to compile

2. Compile and run using g++ in Linux Terminal

Type the following command and press enter to compile the code –

g++ cpp_hello.cpp -o cpp_hello

And then use the following command to run the code –

./cpp_hello

g++ is a c++ compiler that is used here to compile c++ code.

compile the c++ code in a terminal in linux

For more details about the use of gcc and g++ compilers use gcc --help or g++ --help commands in your terminal. You can also write to us in the comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.