Graphics.h in Linux for C and C++

January 8th, 2009

Most of the students in Windows use graphics.h header file in there programs that are written in TurboC. This is a reason that some don’t wanna migrate to Linux well there is a solution for that to. Just follow the steps that i did in Ubuntu..

First run sudo apt-get build-essential 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-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

You can install them via Synaptic Package Manager (System/Administration/) one by one. or type 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”
Now type in the terminal
./configure
sudo make
sudo make install

This completes the installation. :)

Now just add the following lines in your program:
int gd,gm=VGAMAX; gd=DETECT;
initgraph(&gd,&gm,NULL);

and compile using g++ like:
g++ test.cpp -o test.o -lgraph
And execute the program by ./test.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 rectangle) :
#include
#include

int main(void)
{
int gd=DETECT, gm=VGAMAX;
initgraph(&gd, &gm, 0);
moveto(0, 0);
rectangle(50,50,500,200);
while (!kbhit());
closegraph();
return 0;
}

If there are any problems please leave a comment. :)

Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • FriendFeed
  • Reddit
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • Yahoo! Buzz
  • Google Bookmarks

Tags: , ,
Posted in Techi Gyaan | Comments (12)

12 Responses to “Graphics.h in Linux for C and C++”

  1. Savithri Says:

    Way to go !! :)

    Though I am finding it difficult to read. This is for young eyes!!

  2. Abhishek Says:

    OMFG. You don’t even give credit to the person you stole all this from. And you call this blogging. Funny.

  3. Animesh Says:

    Well sorry about dis one.. i 4got to add it.
    Its not from a particular source but a group of sources combined.. :)

  4. azte Says:

    Nice, i used to work on borland C (old i know) but in the school is the only way to make programs.

    This post helps me a lot, i like colors in my linux programs xD.

    Its a shame that you steal content, nah just kidding.

  5. gurkirat Says:

    good work man……uve messed up hard with linux….gr8 hacks….

  6. Animesh Kumar Says:

    Thanxs man… :)

  7. Indus Says:

    Can you help me in displaying a bmp image in linux platform using c?

  8. Vlad Says:

    Thanks, it works excellent!..
    But I have not found the kbhit() function in the graphics.h or library. All variants of kbhit() found using Google worked for me not.
    Did you like to show your kbhit() version or a library which contais it?

  9. Giorgio Says:

    Hey!

    Thanx for the post, what happens with me is that I get this error message:

    graphix.cpp: In function ‘int main(int, char**)’:
    graphix.cpp:39: error: ‘kbhit’ was not declared in this scope

    what is kbhit I guess keyboard hit but how do I define it?

    Thanx!

  10. Anshul Says:

    kbhit is not working it’s giving error: kbhit was not declared in this scope

  11. siddarth Says:

    thanks a lot for explaining the method of installing in a lucid way but
    am getting this error even after installing library
    error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory
    am unable to figure out what is wrong

  12. siddarth Says:

    thanks a lot for posting the command for copying the libraries………….i didnt see it before
    now figured out

Leave a Reply