Monday, August 16, 2010

Adding libgraph, an alternative to graphics.h in ubuntu

hi all, recently we were asked to write graphics programmes in C and we were 'suggested' to use Windows as Linux doesn't support the graphics.h library . This really felt awkward and i knew there must be some way out. By some googling, i found that libgraph was the alternative.
Here's how to install it in ubuntu:
1. Install the libgraph tar.gz file from here.
2. Next you need to install some dependencies , i.e packages:
libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev (For fedora it's SDL-image and SDL-imagedev and the rest two are same)
3. After that you need to extract the downloaded tar and cd to it.
4. Next perform these:
(i). ./configure
(ii) make
(iii) make install
5. Next you need to copy all the libgraph shared files from /usr/local/lib to /usr/lib ,i.e
sudo cp /usr/local/lib/libgraph.* /usr/lib
That's it! now you may run any programme using graphics.h library and see the resutls. ;)

Here's a sample programme to draw a rectangle :


#include stdio.h // please put in angle brackets, there was some problem while posting
#include graphics.h // --do--

int main(void)
{
int gd=DETECT, gm=VGAMAX; // gd is the device driver , gm is the graphics mode
initgraph(&gd,&gm,NULL); // to initialize the graph
moveto(0, 0);
rectangle(50,50,500,200);
while (!kbhit());
closegraph();
return 0;
}


Note:For running the programme you must use the lgraph flag with gcc i.e if the programme is test.c then use gcc test.c -lgraph




For more details you may check out : this
Let the source be open :)

4 comments:

  1. For Fedora please use below command :
    yum install SDL_image SDL_image-devel.i686 guile.i686 guile-devel.i686

    ReplyDelete
  2. yes but don't forget to be the super user and for ubuntu it's:
    sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev

    ReplyDelete
  3. INclude Screenshots...There are many users Who do not understand these tech..s

    ReplyDelete
  4. @RenJith: Thanks for your suggestion. i think the information is self explanatory to someone whose into(or even novice) in graphics programming.The rest are just basic linux commands of installing a package through command lines.Still i could have provided screenshots but presently i'm not into it.

    ReplyDelete