How to Install graphics.h C/C++ library on Ubuntu


In this post we are going to install graphics.h library on Ubuntu and to use it with gcc or g++ compiler. graphics.h provides access to a simple graphics library that makes it possible to draw lines, rectangles, ovals, arcs, polygons, images, and strings on a graphical window. To know about functions in graphic.h library in C/C++ visit here.

To use graphics.hon Ubuntu platform we need to compile and install libgraph. libgraph is an implementation of the TurboC graphics API (graphics.h) on GNU/Linux using SDL.

  1. First we need to add the Universe repository because some packages are not available in main repository.
    1
    2
    
     sudo add-apt-repository universe
     sudo apt-get update
    
  2. Now we will install build-essential and some additional packages.
    For versions prior to 18.4
    1
    2
    3
    4
    5
    
       sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 \
       guile-1.8-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev \
       libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev \
       libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev \
       libslang2-dev libasound2 libasound2-dev build-essential
    

    For 18.04 we need to add repositories of xenial in sources.list.

    1
    
      sudo nano /etc/apt/sources.list
    

    Now add these lines

    1
    2
    
       deb http://us.archive.ubuntu.com/ubuntu/ xenial main universe
       deb-src http://us.archive.ubuntu.com/ubuntu/ xenial main universe
    

    Run sudo apt-get update and then install packages using

    1
    2
    3
    4
    5
    
       sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-2.0 \
       guile-2.0-dev libsdl1.2debian libart-2.0-dev libaudiofile-dev \
       libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev \
       libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev \
       libslang2-dev libasound2 libasound2-dev
    
  3. Download libgraph from here and extract libgraph-1.0.2.tar.gz file.
  4. Go to the extracted folder and run following commands.
    1
    2
    3
    4
    
      ./configure
      make
      sudo make install
      sudo cp /usr/local/lib/libgraph.* /usr/lib
    

Now we can use graphics.h library after adding following lines to the code.

1
2
int gd = DETECT, gm; 
initgraph(&gd, &gm, NULL);

Here is a program of Simple Pendulum Animation on Ubuntu.