Graphics.h in Linux for c/c++


graphics.h is a header file that is used to add graphics in c/c++ program in TURBOC. But in Ubuntu graphics.h file is not supported by g ++ compiler.

For executing graphics program in g++ compiler, we have to install library files.

Steps for installing the graphics library files are as follows:

First run $ sudo apt-get install build-essential in terminal to install necessary compiler tools.

Then, install the following packages:
libsdl-image1.2
libsdl-image1.2-dev
guile-1.8
guile-1.8-dev
libsdl1.2debian-art
libartsc0-dev
libaudiofile-dev
libesd0-dev
libdirectfb-dev
libdirectfb-extra
libfreetype6-dev
libxext-dev
x11proto-xext-dev
libfreetype6(upgrade)
libaa1
libaa1-dev
libslang2-dev
libasound2
libasound2-dev

You can install them by typing the following command in the terminal :
$ sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian-arts libartsc0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6(upgrade) libaa1 libaa1-dev libslang2-dev libasound2 libasound-dev

After this download libgraph-1.0.1 (This is the link) to your Home Folder.
Right click on it and press “Extract here”

open terminal and navigate into the folder u just extracted the file contents into by running $ cd libgraph-1.0.1

Now run the following commands one after the another. Proceed to the next command only if the preceding command executes without any errors.
$ ./configure
  $ sudo make
  $ sudo make install

If you get any errors during the “sudo make” step, paste it in the comments section of this post.

Now technically install is over. :)

Try writing a simple program including graphics.h. Declare the values of gd and gm and call initgraph as follows

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

and when compiling using g++ add an extra paramter -lgraph

i.e if you are compiling a program example.cpp in the terminal u type in

$ g++ example.cpp -o example.o -lgraph

And execute the program by runnung ./example.o

If on compile it gives u an error that says “could not load shared libraries” or something like that just run the following command and it should fix it

$ sudo cp /usr/local/lib/libgraph.* /usr/lib

An example program(to print a line) :

#include<iostream>
#include<graphics.h>

int main()
{
int gd,gm=VGAMAX; gd=DETECT;
initgraph(&gd,&gm,NULL);
line(50,50,80,80);
delay(5000);

return 0;
}

14 thoughts on “Graphics.h in Linux for c/c++

  1. I tried to do it but after installing essentials I couldn’t install all the packages. the command to install those packages does something and in the end it gives this error.
    I think since it couldn’t install all of them I get errors with libgrgraph. What should I do.
    E: Unable to locate package libsdl1.2debian-arts
    E: Couldn’t find any package by regex ‘libsdl1.2debian-arts’
    E: Unable to locate package libartsc0-dev

    • You may install libsdl1.2debian package instead of libsdl1.2debian-arts.
      And then check your program. Is it working or not.
      If not then post error which you got.

      Thanks.

  2. I am getting the following error while running the example program.
    ./gr.out: error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory

    • Create symbolic link for libgraph.so.1 using

      $ ln -s /usr/local/lib/libgraph.so.1 /usr/lib/libgraph.so.1

      Or you can copy that file directly from /usr/local/lib to /usr/local using

      $ cp /usr/local/lib/libgraph.so.1 /usr/lib/libgraph.so.1

      Use sudo with above commands if you got permission error.

Leave a comment