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

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

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

Follow the steps given below

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 command user interface to write our program. Type nano <file_name_with_extension> in the terminal and then press enter.

Write your code inside this file press ctrl+o and enter 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. Basically you can save it anywhere in the system. In this case, you will have to provide the full path of that file while performing any operations on it.

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

2. Compile and run it using gcc

Type gcc <file_name_with_extension> -o <file_name_without_extension> and press enter. Again type ./<file_name_without_extension>and press enter. It will display the output of the program in the terminal. Here gcc is a c compiler that is used here to compile c programs.

Compile And Run C++ Code

All the steps given above will be the same for c++ but we will have to make few changes first one is to use .cpp extension instead of .c for c++ files and g++ compiler in place of gcc compiler.

Follow the step given below:

1. Write your program in c++

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

Write your code inside the file which is opened in the terminal. Press ctrl+o and enter to save the file and 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 g++ <file_name_with_extension> -o <file_name_without_extension> and press enter. Next type ./<file_name_without_extension> and press enter. It will display the output in the terminal. 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

C Style coding standard: All you want to know about

0.0 00 Coding standard makes code writing cleaner and increases readability. C style is one such coding standard that standardizes the way C programs are written. Every big organization follows

C Programming 0 Comments

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

0.0 00 “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

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply