Ubuntu Linux Install GNU GCC Compiler and Development Environment

See all Ubuntu Linux related FAQ
How do I install GNU/GCC (C and C++) compiler and related tools (such as make, debugger, man pages) collection under Ubuntu Linux operating system using command line options?

You need to install following packages on Debian and Ubuntu Linux:
build-essential package – Installs the following collection to compile c/c++ program on a Ubuntu Linux including:
Advertisement

  1. libc6-dev – C standard library.
  2. gcc – C compiler.
  3. g++ – C++ compiler.
  4. make – GNU make utility to maintain groups of programs.
  5. dpkg-dev – Debian package development tools.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Package Manager
Prerequisites Ubuntu
OS compatibility Debian Mint Pop!_OS Ubuntu
Est. reading time 5 minutes
Basically, build-essential package contains an informational list of packages which are considered essential for building Ubuntu packages including gcc compiler, make and other required tools. This package also depends on the packages on that list, to make it easy to have the build-essential packages installed. In this tutorial, you will learn about installing the GNU C compiler and GNU C++ compiler on a Ubuntu Linux.

Installing compilers using apt command

Open the terminal app and type the following apt command/apt-get command:
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install build-essential

OR
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential

Sample outputs:

Fig.01: How do I install development tools for Ubuntu Linux?

Fig.01: How do I install development tools for Ubuntu Linux?

Verify installation

Type the following commands:
$ whereis gcc make
$ gcc --version
$ make -v

Fig.02: Finding out installed make and gcc version

Fig.02: Finding out installed make and gcc version

Installing the dev man pages on a Ubuntu Linux

Type the following command:
$ sudo apt-get install manpages-dev man-db manpages-posix-dev
To view library calls (functions within program libraries), enter:
$ man 3 scanf
$ man 2 execve
$ man 2 fork

You can write a small program to test GNU c/c++ compiler:
$ vi test.cpp
Append the following code:

#include <iostream>
// My first program
using namespace std;
int main(){
	cout << "Hello, World!\n";
    return 0;
}

Save and close the program. You can compile it as follows:
$ make test
OR
$ g++ test.cpp -o test
You should get an executable named test in the current directory:
$ ls -l test
Sample outputs:

-rwxr-xr-x 1 vivek vivek 8768 Dec 11 14:49 test

Just run it:
$ ./test

Writing sample C program on Ubuntu Linux

The following C code on Ubuntu will print the current computer name, date & time and ask a user to enter their name. It will also do simple validation for user input. This code is complicated for a new user, but it should be easy to understand if you know a bit of C language. I documented the code for ease of understanding.

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

/*
 Name: hello.c
 Purpose: Show user the current computer name, date/time and ask for their name.
 Author: Vivek Gite 
*/
int main() {
    char flname[100]; /* store first and last name */
    char my_hostname[1024]; /* store computer name */
    /* Store date and time */
    time_t c_time; 
    struct tm *l_time;
    
    /* Get the computer name */
    gethostname(my_hostname, 1024);

    /* Get the current time */
    time(&c_time);
    l_time = localtime(&c_time);

    /* Show the current computer name and date/time on user screen */
    printf("\n*** The current date is '%02d/%02d/%04d' (%02d:%02d:%02d) on '%s' hostname. ***\n", l_time->tm_mday, l_time->tm_mon+1, 
           l_time->tm_year+1900, l_time->tm_hour, l_time->tm_min, l_time->tm_sec,  my_hostname);

    /* Ask for input */
    printf("\nEnter your name : "); /* ask for input */
    fgets(flname, sizeof(flname), stdin);

    /* Remove the newline character from the end of the input string */
    flname[strcspn(flname, "\n")] = '\0';   

    /* Validate input */ 
    if (strlen(flname) == 0) {
        printf("\nYou did not enter your name.\n");
    } else {
        printf("\nHello, %s! Let us be friends!!\nBye\n", flname);
    }
    
    return 0;
}

Compile it as follows:
$ cc hello.c -o hello
## OR ##
$ make hello

Try it:
$ ./hello

Ubuntu Linux Install GNU GCC Compiler and Development Environment and test it with C

Click to enlarge

Installing the X11 development compilers

Type the following command:
$ sudo apt install libx11-dev
Outputs:

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpthread-stubs0-dev libxau-dev libxcb1-dev libxdmcp-dev x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
Suggested packages:
  libx11-doc libxcb-doc
The following NEW packages will be installed:
  libpthread-stubs0-dev libx11-dev libxau-dev libxcb1-dev libxdmcp-dev x11proto-core-dev x11proto-dev xorg-sgml-doctools xtrans-dev
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,446 kB of archives.
After this operation, 5,554 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://in.archive.ubuntu.com/ubuntu focal/main amd64 libpthread-stubs0-dev amd64 0.4-1 [5,384 B]
Get:2 http://in.archive.ubuntu.com/ubuntu focal/main amd64 xorg-sgml-doctools all 1:1.11-1 [12.9 kB]
.....
..
Fetched 1,446 kB in 2s (748 kB/s)   
Selecting previously unselected package libpthread-stubs0-dev:amd64.
(Reading database ... 271801 files and directories currently installed.)
Preparing to unpack .../0-libpthread-stubs0-dev_0.4-1_amd64.deb ...
Unpacking libpthread-stubs0-dev:amd64 (0.4-1) ...
Selecting previously unselected package xorg-sgml-doctools.
Preparing to unpack .../1-xorg-sgml-doctools_1%3a1.11-1_all.deb ...
....
...
Unpacking libx11-dev:amd64 (2:1.6.9-2ubuntu1.2) ...
Setting up libpthread-stubs0-dev:amd64 (0.4-1) ...
Setting up xtrans-dev (1.4.0-1) ...
Setting up xorg-sgml-doctools (1:1.11-1) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for sgml-base (1.29.1) ...
Setting up x11proto-dev (2019.2-1ubuntu1) ...
Setting up libxau-dev:amd64 (1:1.0.9-0ubuntu1) ...
Setting up libxdmcp-dev:amd64 (1:1.1.3-0ubuntu1) ...
Setting up x11proto-core-dev (2019.2-1ubuntu1) ...
Setting up libxcb1-dev:amd64 (1.14-2) ...
Setting up libx11-dev:amd64 (2:1.6.9-2ubuntu1.2) ...

Drawing a simple circle using X11 and C program

Create circle.c as follows:

#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
// ----------------------------------------------------
// Name: circle.c
// Purpose: Draw a circle using X11
// ----------------------------------------------------
// Set Width, Height and RADIUS for circle here
#define WIDTH  300
#define HEIGHT 300
#define RADIUS 100

// Set X11 color here. It is defined using X11 hex format
#define LIME_GREEN_COLOR 0x32CD32
#define DEEP_PINK_COLOR 0xFF1493
#define TAN_COLOR 0xD2B48C

int main() {
    // Set X11 data structures for Display, Window, Event, Graphic Context and Screen
    Display *xdisplay;
    Window xwindow;
    XEvent xevent;
    GC graphics_context;
    int my_screen;
    
    // Connect to the X server and get the default screen here
    xdisplay = XOpenDisplay(NULL);
    if (xdisplay == NULL) {
        fprintf(stderr, "Error: I cannot open display\n");
        exit(1);
    }
    my_screen = DefaultScreen(xdisplay);
    
    // First, create a new window
    xwindow = XCreateSimpleWindow(xdisplay, RootWindow(xdisplay, my_screen), 0, 0, WIDTH, HEIGHT, 1,
                            BlackPixel(xdisplay, my_screen), WhitePixel(xdisplay, my_screen));
    
    // Select the events to listen for
    XSelectInput(xdisplay, xwindow, ExposureMask | KeyPressMask);
    
    // Display the window
    XMapWindow(xdisplay, xwindow);
    
    // Create a new graphics context
    graphics_context = XCreateGC(xdisplay, xwindow, 0, NULL);
    
    // Set the foreground color to TAN
    XSetForeground(xdisplay, graphics_context, TAN_COLOR);
    
    // Draw a circle in the center of the window 
    XFillArc(xdisplay, xwindow, graphics_context, WIDTH/2-RADIUS, HEIGHT/2-RADIUS, 2*RADIUS, 2*RADIUS, 0, 360*64);
    
    // We handle events here using while() loop
    while (1) {
        XNextEvent(xdisplay, &xevent);
        if (xevent.type == Expose) {
            // Redraw the circle if the window is exposed
            XFillArc(xdisplay, xwindow, graphics_context, WIDTH/2-RADIUS, HEIGHT/2-RADIUS, 2*RADIUS, 2*RADIUS, 0, 360*64);
        }
        if (xevent.type == KeyPress) {
            // Exit this app if a key is pressed
            break;
        }
    }
    
    // Final clean up is done here
    XFreeGC(xdisplay, graphics_context);
    XDestroyWindow(xdisplay, xwindow);
    XCloseDisplay(xdisplay);
    
    return 0;
}

Compile it as follows:
$ cc circle.c -o circle -lX11
Run it:
$ ./circle

circle.c outputs

Output from circle.c

Summing up

And that is how you install and test the GNU GCC C/C++ compiler on your Ubuntu Linux machine. You can write C or C++ code using VIM, Emacs or any other awesome text editor mentioned here. Professional developers may find using C & C++ developer tools from JetBrains practical, too—the choice of text editor/IDE depends upon your needs.

This entry is 1 of 13 in the Linux GNU/GCC Compilers Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Install GNU GCC Compiler and Development Environment
  2. Debian Linux Install GNU GCC Compiler and Development Environment
  3. CentOS / RHEL 7: Install GCC (C and C++ Compiler) and Development Tools
  4. Download and Install C, C++ Compiler on Red Hat Enterprise Linux 5 (RHEL)
  5. Mac OS X: Install GCC Compiler with Xcode
  6. Where is My Linux GNU C or GCC Compilers Are Installed?
  7. HowTo: Compile And Run a C/C++ Code In Linux
  8. RHEL / CentOS Linux Install Core Development Tools Automake, Gcc (C/C++), Perl, Python & Debuggers
  9. HowTo Compiling C Program And Creating Executable File Under a Linux / UNIX / *BSD
  10. How To Install ncurses Library on a Linux
  11. Linux Find Out What Compilers Are Installed or Available On The System
  12. Linux Find Out GNU gcc Compiler Version Used To Compile Running Kernel
  13. Howto see output of C program in Linux or UNIX

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

2 comments… add one
  • Abhay Feb 24, 2023 @ 1:37

    Very helpfiul

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.