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

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

   

This post was last updated on August 3rd, 2020 at 03:14 pm

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.

Compile And Run C Code

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

Write your code inside this file and then press ctrl+s to save the file and ctrl+x to exit from the editor. 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.

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

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 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.

2. Compile and run using g++

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.

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.

Previous Timeshift v19.08
Next How To Use Vi Text Editor An Easy Guide

About author

You might also like

C Programming 0 Comments

C Style coding standard: All you want to know about

This post was last updated on January 22nd, 2019 at 11:02 amCoding standard makes code writing cleaner and increases readability. C style is one such coding standard that standardizes the

C Programming 0 Comments

Divided By Zero: What Happens When it Occurs on a System

“Divided by Zero” exception, what is this? This is a good question and you must know the answer which will lead you to know a system better. When I was

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply