Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linux
3.
Installation of C/C++ Compiler In Linux
4.
Steps of compiling C/C++ Code In Linux
4.1.
For C++
4.2.
For C
5.
Features of Linux
6.
Frequently Asked Questions
6.1.
What is the GCC compiler in Linux?
6.2.
Can I compile C and C++ together?
6.3.
Can I use gcc to compile C++ code?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Compile And Run a C/C++ Code In Linux

Author Muskan Sharma
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Most of you use Linux in your systems because of its features like multiprogramming, better security, and open source. 
But have you ever thought about compiling and running your C/C++ code in Linux?

Don’t worry!! You’re at the right place.

Compile And Run a C/C++ Code In Linux

Linux is an open-source operating system built up of the kernel, the OS's foundation, and the tools, programs, and services that come with it.
This article will help you clear all your doubts about the process of compiling and running a C/C++ code in Linux.

So let us begin to explore more about this topic.

Linux

Linux is a family of open-source Unix-like operating systems based on the Linux kernel, which was developed by  Linus Torvalds and initially released on September 17, 1991. The kernel and accompanying system software and libraries, many of which are given by the GNU (GNU's Not Unix) Project, are often packaged as a Linux distribution. 

Linux is more secure than many other operating systems and does not require antivirus software to be protected. Linux is also very stable, takes up little disc space, has extensive networking capabilities, and puts software updates in the hands of the user.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Installation of C/C++ Compiler In Linux

Below are the steps for installing C/C++ Compiler In Linux.

Step 1: To install  C/C++ Compiler if you are using Debian or Ubuntu Linux. Type the command the following command.
 

$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev
command to install to C/C++ compiler in Linux
command to install to C/C++ compiler in Linux


Step 2: Run the following command on Linux to find the compiler's version number and location on your device.

$ whereis gcc
$ which gcc
$ gcc --version
command to get the version and location of linux

Steps of compiling C/C++ Code In Linux

Now let us look at the steps for compiling and running C/C++ code in Linux.

For C++

Step 1: To compile and run a C/C++ code, create a text file called a codingninjas1.c with the help of any text editors of your choice.
 

text file naming codingninjas.c for C++


Step 2: Now, we are checking whether our text file has been created or not.

command to check whether text file is created or not

Step 3: In this step, we have executed our code.

successful execution of code

For C

The same steps will be followed for the C code.

Step 1: To compile and run a C/C++ code, create a text file called a codingninjas.c with the help of any text editors of your choice.

text file naming codingninjas.c for C


Step 2: Now, we are checking whether our text file has been created or not.

command to check whether text file is created or not


Step 3: In this step, we have executed our code.

successful execution of code


Alright!! Now let us look at the features of Linux.

Features of Linux

Some of the Features of linux operating system are discussed below.

  • Open-Source: Linux code is freely available to anybody. It is a community-based development effort allowing users to alter, analyze and redistribute the code.
     
  • Portability: Software portability means it can run similarly on different types of hardware. The Linux kernel and application programs can be installed on any hardware platform.
     
  • Security: Linux is more secure as it provides many features like authentication, authorization, and encryption for data security.
     
  • Multiuser Capacity: Users in Multiuser Capacity can share system resources such as RAM, hard drive, and application programs at the same time. However, they must use it in many terminals.
     
  • Multiprogramming: Linux is a multiprogramming system. In Linux, numerous applications can execute at the same time.

Frequently Asked Questions

What is the GCC compiler in Linux?

The GNU Compiler Collection (GCC) is a compiler system developed by the GNU Project that supports various computer languages. GCC is an important component of the GNU toolchain and the standard compiler for most GNU and Linux projects, including the Linux kernel.

Can I compile C and C++ together?

It is important that the compilers used to compile C and C++ together are compatible in order to correctly mix them. This means they should define basic types like int and float in the same way. 

Can I use gcc to compile C++ code?

Yes, you can use GCC to compile C++ code. GCC stands for GNU Compiler Collections, which primarily compiles C and C++ code. It is also capable of compiling Objective C and Objective C++.

Conclusion

In this article, you have learned about how to compile and run a C/C++ code in Linux, the steps for installing and compiling C/C++ code in Linux, and the features of Linux.We hope this article briefly helped you learn about compiling and running a C/C++ code In Linux.
If you want to learn more, refer to these articles:

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!
 

Happy Learning!

Previous article
Tar Command in Linux with Examples
Next article
Windows Operating System
Live masterclass