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 How to Copy Directory in Linux / Unix
Next How To Use Vi Text Editor An Easy Guide
 

About author

You might also like

C Programming 0 Comments

Writing the First Hello World Program in C

Even the huge buildings, shopping complex etc. are built brick by brick. Isn’t that so? Similarly to learn the basics of C language you need to start from the very

C Programming 0 Comments

Understanding C Programming Basics

So, finally you decided to learn C programming either because it is in your academic syllabus or you to get a job or it is really interesting to you. Whatever

C Programming 0 Comments

How to Compile and Run C Program on a Linux Terminal

This post was last updated on April 17th, 2021 at 11:08 pmLinux is the best platform to learn C programming because it is an open source platform and you will

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply