How to use graphics.h in Ubuntu?

Semester 7 at college came up with a bunch of new and interesting stuff. One of them is the computer graphics lab, where we all got to do simple graphic programming in C.

Soon enough, this turned out to be a bit uneasy.
The reason was that we were forced to work in Windows, since there is no support for graphics.h in the gcc compiler in Ubuntu. To make things worse, the computers had Windows 7, and we had to install a simulator called DOS-box to run the programs properly. All this felt so messy and I just wanted to run back to the comfort of programming in Ubuntu!

Then there was some solution here. Basically, we've to install some packages and libraries which enable us to compile graphics.h programs in linux. The original article is a bit old and many packages mentioned are obsolete now. So I've written a patched up, more up-to-date version of the same.
Hope this saves you from going back to the bitter blue screen of Turbo C ;)

Click image to enlarge

Step #0
Make sure you have the basic compilers installed.
You need the build-essential package. For this, run the command:  

sudo apt-get install build-essential

Step #1
First we need to install a hand full of packages. You can simply run the following command and get it all done.   

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian-all 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

Step #2
Now, download libgraph.

Copy the file libgraph-1.0.2.tar.gz to our home folder. Right click on the file and select Extract here.

Open a terminal and run the following commands, one by one. 

cd libgraph-1.0.2
sudo make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib

Step #3
Now you're ready to compile your C program!

You have to type in the line #include<graphics.h> , just like you'd do in Windows.

In programs using graphics.h in Turbo C, you'll be using something like this:

int gd=DETECT,gm;

In Ubuntu, you replace the "c:\\tc\\bgi" part with a NULL

int gd=DETECT,gm;

Finally, while compiling the C file, don't forget to add a -lgraph parameter. The code will be something like this:

gcc heart.c -o heart -lgraph

And there you go! :)
Feel free to use the example code I made: heart.c

Was this post helpful? Let us know:

Although this implementation helps us to run all graphics.h functions, some predefined constants like SOLID_FILL may not be supported. Anyways, we can use integers instead of such names. This fixes the problem without much effort.

The graphics window can occasionally crash on focus. Rather than a bug in the graphics.h implementation, this is a common bug in gnu/linux graphics which apparently affects many other applications.