banner



How To Install C And C++ In Ubuntu

Acquire how to install C on Linux.

Overview

Linux is a set of open-source UNIX-like operating systems, and Ubuntu is a Linux-based operating organisation commonly used to run Linux-based applications. To install C on Linux and to build and run our C programme file on Ubuntu, we need to install the GCC Compiler. In Ubuntu repositories, GCC Compiler is a role of the build-essential package we demand to install.

We empathize that installing C on Linux might seem daunting, only don't worry; we've got yous covered with our step-by-step guide!

Scope of Commodity

In this commodity, we will encounter

  • How to install C and GNU Compiler Collection (GCC Compiler) on Linux.
  • How to compile and run a C Programme in the Linux terminal.

Installing the GCC Compiler Using Terminal in Linux

GCC Compiler (GNU Compiler Collection) is a collection of compilers and libraries for the programs written in C, C++, Ada, Become, D, Fortran, and Objective-C programming languages and it is distributed under the GNU General Public License. We are going to install the GCC compiler using the Linux terminal to install C on Linux.

The GCC Compiler, and several other libraries and utilities required for building applications, can be found in the default Ubuntu repositories (Ubuntu is a Linux-based Operating System) under the build-essential meta-package.

What is the build-essential Packet?

This build-essential meta-packet on Ubuntu comes with 5 separate packages that are required during a software compilation process, and it contains the GCC compiler that will exist used to build and run your C Programs. These five packages are listed below:

  • gcc : The GNU Compiler Collection (GCC Compiler) is used to compile programs written in C Linguistic communication.
  • g++ : The GNU C++ compiler is used to compile programs written in C++ Language.
  • libc6-dev : This is the GNU library files packet. The evolution libraries and header files needed to compile unproblematic C and C++ programs are included in this package.
  • make : This is a handy tool for controlling the compilation of programme. This tool reads a file called a "makefile", which tells the compiler how to do its task.
  • dpkg-dev : This bundle tin can be used to unzip, compile, and upload Debian source packages. If y'all wish to package your program for a Debian-based system, this tool volition come in handy.

So, nosotros accept to perform the following steps to install the build-essential package in our Linux operating organization:

First, we take to open up the last; we can apply the shortcut Ctrl + Alt + T or we tin can manually open up information technology upward past searching information technology in the menu ( ⋮⋮⋮ icon).

ane. To begin, update the packages listing using the below command:

          

The apt control in Linux is in charge of installing, uninstalling, and updating applications on our arrangement. Consider your calculator to be a factory, and the apt command to be the manufacturing plant managing director who manages the installation of new equipments, removing obsolete equipments, and updating the desired equipments to the latest versions while keeping track of the equipment names and versions currently in use. And so, sudo apt update command is used to update the rails of the software versions that helps in installing the newest available software version in your systems.

Note:

sudo (Super User DO) enables a permitted user to run a command as the superuser or another user, depending on the security policy. So, if required, enter your system password to go along.

OUTPUT:

            
                                  scaler@scaler-topics:~$ sudo apt update                                    [sudo] password                                    for                                      scaler:                                    Hit:                  1                                      http:                  //in.archive.ubuntu.com/ubuntu focal InRelease                                                      Get:                  2                                      http:                  //in.archive.ubuntu.com/ubuntu focal updates InRelease [114 kB]                                                                        Become:                  3                                      http:                  //tn.archive.ubuntu.com/ubuntu focal backports InRelease [108 kB]                                                                        Get:                  4                                      http:                  //security.ubuntu.com/ubuntu focal-securtty InRelease [114 kB]                                                                        Become:                  5                                      http:                  //in.archive.ubuntu.com/ubuntu focal updates/chief amd64 Packages [1, 468 kB]                                                      Get:                  vi                                      http:                  //in.annal.ubuntu.com/ubuntu focal-updates/matn 1386 Packages [585 kB]                                                      Get:                  seven                                      http:                  //in.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [291 kB]                                                      Go:                  8                                      http:                  //in.archive.ubuntu.com/ubuntu focal updates/principal amd64 DEP-11 Metadata [279 kB]                                                      Get:                  9                                      http:                  //in.annal.ubuntu.com/ubuntu focal-updates/main DEP-eleven 48x48 Icons [60.8 kB]                                                      Become:                  10                                      http:                  //in.archive.ubuntu.com/ubuntu focal-updates/main DEP-xi 64x64 Icon s [98.3 kB]                                                      Get:                  11                                      http:                  //tn.annal.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metad ata [14.vii kB]                                                      Get:                  12                                      http:                  //in.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packa ges [694 kB]                                                      Become:                  13                                      http:                  //tn.annal.ubuntu.com/ubuntu focal-updates/restricted Translation -en [99.0 kB]                                                                        Get:                  14                                      http:                  //tn.annal.ubuntu.com/ubuntu focal-updates/untverse 1386 Packages [663 kB]                                                      Go:                  xv                                      http:                  //in.annal.ubuntu.com/ubuntu focal-updates/universe amd64 Package s [892 kB]                                                      Go:                  sixteen                                      http:                  //tn.annal.ubuntu.com/ubuntu focal-updates/universe Translation-e north [196 kB]                                    ...                                                 

2. Install the packet build-essential using the following control:

            
                                  $ sudo apt install build-essential                                                

If required enter your organisation password to proceed.

OUTPUT:

            
                                  scaler@scaler-topics:~$ sudo apt install build-essential                                    [sudo] password                                    for                                      scaler:                  Reading package lists... Done                   Edifice dependency tree                          Reading state information... Done                   The following additional packages will be installed:                                      binutils binutils-common binutils-x86                  -64                  -linux-gnu dpkg-dev fakeroot g++ chiliad++                  -9                                      gcc                                                        gcc                  -9                                      libalgorithm-diff-perl libalgorithm-diff-xs-perl libalgorithm-merge-perl                                    libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev libcrypt-dev libctf-nobfd0                                      libctf0 libfakeroot libgcc                  -ix                  -dev libitm1 liblsan0 libquadmath0 libstdc++                  -nine                  -dev                                    libtsan0 libubsan1 linux-libc-dev make manpages-dev                   Suggested packages:                                      binutils-doc debian-keyring yard++-multilib chiliad++                  -9                  -multilib gcc                  -9                  -dr. gcc-multilib                                                        autoconf automake libtool flex bison gcc-doc gcc                  -nine                  -multilib gcc                  -nine                  -locales glibc-doc                                                        libstdc++                  -9                  -doc make-doc                  The following NEW packages volition exist installed:                                      binutils binutils-common binutils-x86                  -64                  -linux-gnu build-essential dpkg-dev                                                        fakeroot g++ thousand++                  -9                                      gcc gcc                  -9                                      libalgorithm-unequal-perl libalgorithm-diff-xs-perl                                    libalgorithm-merge-perl libasan5 libatomic1 libbinutils libc-dev-bin libc6-dev                                      libcrypt-dev libctf-nobfd0 libctf0 libfakeroot libgcc                  -9                  -dev libitm1 liblsan0                                                        libquadmath0 libstdc++                  -9                  -dev libtsan0 libubsan1 linux-libc-dev make manpages-dev                                                      0                                      upgraded,                                    32                                      newly installed,                                    0                                      to remove                                    and                                                      259                                                      not                                      upgraded.                                    Need to get                                    3518                                      kB/                  34.iv                                      MB of athenaeum.                                    After                                    this                                      operation,                                    157                                      MB of boosted deejay infinite will be used.                                    Exercise you want to                                    continue                  ? [Y/n]                                                

Printing the Y key and and so Enter key in one case you go the similar output as above on your screen to continue the installation procedure.

It will take 3-5 minutes to install the build-essential meta-package completely. This process will install C on Linux operating system and we tin can use the GCC compiler to compile and run our C Programs.

Congratulations! Now y'all accept completed the installation of the build-essential bundle and the GCC Compiler (C Lanuguage) in Linux.

Annotation:

We are using Ubuntu 20.04 LTS version in this tutorial. This version of Ubuntu comes with a pre-installed version of the GCC compiler, then you tin can outset check the installed compiler version using the command mentioned in the next department.

For Reddish Chapeau Linux / Fedora / CentOS

There is no build-essentials parcel in Scarlet Lid Linux, Fedora or CentOS simply we can install C on Linux through a like bundle in these operating systems required for software development. So, if you are using a Ruby-red Hat Linux / Fedora / CentOS based Linux operating organization, and so you accept to install the Development Tools package to get the GCC compiler in your system.

1. Kickoff update the packages list using the beneath command as root user:

          

2. Install Development Tools using the below control equally root user:

            
                                  # yum groupinstall                                    'Development Tools'                                                                  

or

            
                                  $ sudo yum group install                                    "Development Tools"                                                                  

Check the Installed Compiler Version

Now that you have installed the build-essentials / Development Tools package, you tin can easily check/validate your GCC Compiler version. In this section, you will run into how y'all tin can check/validate the installed GCC compiler version by using the below command in your Linux terminal:

        

Note:

--version flag nether the gcc command is compatible with nearly all the terminal evolution software in Linux.

OUTPUT:

            
                                  scaler@scaler-topics:~$ gcc --                  version                                                      gcc                                                      (Ubuntu                                    nine.3                  .0                  -17u                  buntu1~                  20.04                  )                                      9.3.0                                                      Copyright                                                      (C)                                      2022 Gratis Software Foundation, Inc.                                    This is                                    free                                      software                  ; run into the source                                    for                                      copying atmospheric condition.  In that location is NO                                    warranty;                                    not                                      fifty-fifty                                    for                                      MERCHANTABILITY                                    or                                      Fitness FOR A PARTICULAR PURPOSE.                                                

If you see an output like the higher up-displayed output on your screen, you lot are good to go.

Note:

If the output looks similar the below output, in that location was some discrepancy during the installation procedure. Echo the steps mentioned to a higher place to install build-essential packet again.

            
                                  scaler@scaler-topics:~$ gcc --version                                    Command                                    'gcc'                                                      not                                      found, but can be installed with:                                                      sudo apt install gcc                                                 

Creating a C Program

In this section, you will to create a simple C program on Linux using a pre-installed text editor.

There are many Linux terminal commands to create a new file like using the touch, > (redirect operator), cat, repeat commands, or terminal editors similar half-dozen, vim, or nano. We are using the touch on command in this tutorial in the final to create an empty C plan file. Before creating a new file, you can also alter the directory of the terminal to whatever desired location in your system. We have called the Desktop directory in this tutorial. You can modify to Desktop directory using cd Desktop (modify directory to Desktop ) control or you can choose any directory of your option, just pass the location address of the directory afterwards the cd command. You have to make sure that the C programme file is compiled in the same directory as where the C programme file is present.

Let's meet this in action in Ubuntu 20.04 OS.

Step 1: touch howdy.c control in the terminal volition create an empty hullo.c C program file in the desktop directory.

creating a c program on linux step one

Step two: Open up the hullo.c file in the in-congenital text editor of Linux (or yous can use whatever editor of your choices similar Sublime Text or Visual Studio Code) and type the Howdy Earth C program in the editor equally given below.

Howdy World Programme in C

            
                                  // C Plan to impress Hi, Globe! in the output                                                                        #                  include                  <stdio.h>                                                                        int                                                      principal                  ()                                                                        {                                                                        // printf function is used to print the cord argument in the output window                                                                                          printf                  (                  "Hello, World!\n"                  );                                                                        render                                                      0                  ;                  }                                                 

Y'all tin can also check and run this program here. (IDE past InterviewBit)

OUTPUT:

Hello, World!

creating a c program on linux step two

Congratulations! You're at present merely i footstep away from compiling a C program nether the Linux operating system. And so far, You lot have created a new file hullo.c through the last and successfully written the Hello World programme in the in-congenital text editor.

At present let the states see how to compile the above C plan file in the Linux terminal.

Compiling and Executing the C Program with GCC Compiler

To compile our C Program file, we can apply the below control in the final (use this control in the same directory where the hello.c file is nowadays):

          

The in a higher place control will generate an executable file with the name hello equally given in the command after -o . We can give any name to the executable file. It is non necessary that we give the same name as the C program file.

Annotation:

We also take to ensure that the above command is used in the same directory where the hello.c file is present.

Now to run this executable file, we just demand to run the below command and the output of the C program will exist shown on the terminal screen.

          

This command will requite Hello, Earth! output on the last, if you lot accept written the same C program as mentioned in the higher up section.

Notation :

Use this command in the same directory where the hello executable file is present.

Let'southward run across this in action in Ubuntu twenty.04 OS.

Step 1: Use gcc hello.c -o hello control to compile the hello.c program and generate a hello executable file. We can see from the paradigm that a how-do-you-do file is created in the Desktop directory.

creating a c program on linux step three

Pace 2: To run the executable file simply enter the ./hi command and output volition be visible on the last screen as shown in the beneath paradigm.

Note:

Apply this control in the aforementioned directory where the hello executable file is nowadays. For example, we are compiling and executing our program file on the Desktop directory.

This command will give Hello, Globe! output on the terminal, if you accept written the same C program every bit mentioned in the above section.

creating a c program on linux step four

Congratulations!: You lot accept successfully completed the compilation and running process of a C Plan nether a Linux Operating System.

Determination

  • We learned how to install C on Linux.
  • Nosotros need to install the build-essential / Development Tools packet to install C on Linux and become the GCC Compiler in the system.
  • build-essential meta-package comes with 5 separate packages that are required during a software compilation process i.due east. gcc , grand++ , libc6-dev , make and dpkg-dev .
  • GCC compiler is used to compile and run a C plan on the Linux operating system.
  • Nosotros can use the in-build text editor or other editors like Visual Studio Code or Sublime Text to write our C Programs.

Source: https://www.scaler.com/topics/c/install-c-on-linux/

Posted by: branchcolood.blogspot.com

0 Response to "How To Install C And C++ In Ubuntu"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel