Installing C and C++ compiler (GNU GCC Compiler) On Ubuntu/Debian Linux
This post will guide you how to install the GNU C compiler and GNU C++ compiler under Ubuntu/Debian Linux. How do I install C, C++ compiler and Development Tools on Ubuntu 16.04/18.04 Linux. How to Install GNU GCC Compiler and Development Environment under Ubuntu/Debian Operating system. How to install the manpages for C and C++ development on Ubuntu.
What is GCC?
GCC is the GNU compiler collection, and it includes front ends for C, C++, Objective-C, Fortran, Ada, and Go, as well as libraries for these languages (libstdc++,…). GCC was originally written as the compiler for the GNU operating system.
To install GCC and G++ compiler, You need to install the build-essential package, and this package contains a list of packages which are essential for building Ubuntu Development including GCC compiler, G++ compiler, Make, dpkg-dev and other packages.
GCC is the compiler of C program, and G++ is the compiler of C++ program.
Installing Compilers on Ubuntu
You can simply install build-essential package using apt-get command on Ubuntu/Debian Linux, Type:
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install build-essential
Or you can also use the apt command to install package, type:
$ sudo apt update $ sudo apt upgrade $ sudo apt install build-essential
Outputs:
devops@devops-osetc:~$ sudo apt install build-essential sudo: unable to resolve host devops-osetc [sudo] password for devops: Reading package lists... Done Building dependency tree Reading state information... Done build-essential is already the newest version (12.1ubuntu2). 0 upgraded, 0 newly installed, 0 to remove and 599 not upgraded.
Now you should able to compile C/C++ programs using gcc/G++ compilers.
Checking Versions of Compiler
After you installed the build-essential package on your Ubuntu/Debian Linux system, you can check the installed version of gcc or g++ compiler to check if the compilers are installed properly. Type:
$ gcc --version
Outputs:
devops@devops-osetc:~$ gcc --version gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
or
$ g++ --version
Outputs:
devops@devops-osetc:~$ g++ --version g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Or
$ make --version
Outputs:
devops@devops-osetc:~$ make --version GNU Make 4.1 Built for x86_64-pc-linux-gnu Copyright (C) 1988-2014 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
Installing the manpages for C and C++ development
To install the manpages for c and c++ development, you can install manpages-dev package, type:
$ sudo apt-get install manpages-dev
Outputs:
devops@devops-osetc:~$ sudo apt-get install manpages-dev sudo: unable to resolve host devops-osetc Reading package lists... Done Building dependency tree Reading state information... Done manpages-dev is already the newest version (4.04-2). 0 upgraded, 0 newly installed, 0 to remove and 599 not upgraded.
Compile C and C++ Program with GNU GCC Compiler
Let’s write down a simple C program to test C compiler, open your vi text editor and appending the following code, and save as fio.c:
#include<stdio.h> int main() { printf("hello world"); return 0; }
Save and close the file. and then execute the below command to compile the fio.c program as an executable file named as fio.type:
$ gcc fio.c -o fio
then you can run the C profile, type:
# ./fio
Outputs:
devops@devops-osetc:~$ gcc fio.c -o fio devops@devops-osetc:~$ ./fio hello world
You can also write a C++ program named fio.cc via vim editor, and append the following code:
#include <iostream> using namespace std; int main() { cout << "Hello world!"; return 0; }
Save and close the file fio.cc, and executing the following command to compile and execute it:
$ g++ -o fio fio.cc $ ./fio
Outputs:
devops@devops-osetc:~$ vim fio.cc devops@devops-osetc:~$ g++ -o fio fio.cc devops@devops-osetc:~$ ./fio Hello world!