Animesh Kumar

Animesh Kumar {itsAnimesh}

I'm an IT professional from New Delhi, India. Currently for the most part I work as a Technical Consultant developing ICT solutions for social and societal inclusion. I'm also pursuing PG in Management. Below are the latest updates from a some social networks I subscribe to.

Graphics.h in Linux for C and C++

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. :)

Thanks for Reading... Please Post Your Comments Below

  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. 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.

  4. gurkirat says:

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

  5. Indus says:

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

  6. 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?

  7. 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!

  8. Anshul says:

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

  9. 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

  10. siddarth says:

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

  11. Sachin says:

    Well i dnlded the packages n xtracted the libgraph-1.0.1 package to home directory.. bt the rest is not working. When i type ./configure it saye “No such file or directory”.. Pls reply

  12. juggernaut says:

    i used this blog earlier to use graphics in 9.10 ubuntu…i switched to 10.04.1 (Lucid) but this thing is givin me a headache…

    when i “sudo make”

    Makefile:934: warning: overriding commands for target `libgraph.pc’
    Makefile:409: warning: ignoring old commands for target `libgraph.pc’
    make all-recursive
    make[1]: Entering directory `/home/vmetal/libgraph-1.0.2′
    Makefile:934: warning: overriding commands for target `libgraph.pc’
    Makefile:409: warning: ignoring old commands for target `libgraph.pc’
    Making all in doc
    make[2]: Entering directory `/home/vmetal/libgraph-1.0.2/doc’
    Making all in man
    make[3]: Entering directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[3]: Nothing to be done for `all’.
    make[3]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[3]: Entering directory `/home/vmetal/libgraph-1.0.2/doc’
    make[3]: Nothing to be done for `all-am’.
    make[3]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc’
    make[2]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc’
    make[2]: Entering directory `/home/vmetal/libgraph-1.0.2′
    Makefile:934: warning: overriding commands for target `libgraph.pc’
    Makefile:409: warning: ignoring old commands for target `libgraph.pc’
    make[2]: Leaving directory `/home/vmetal/libgraph-1.0.2′
    make[1]: Leaving directory `/home/vmetal/libgraph-1.0.2′
    vmetal@ubuntu:~/libgraph-1.0.2$

  13. juggernaut says:

    and “make install” gives….

    vmetal@ubuntu:~/libgraph-1.0.2$ make install
    Makefile:934: warning: overriding commands for target `libgraph.pc’
    Makefile:409: warning: ignoring old commands for target `libgraph.pc’
    make install-recursive
    make[1]: Entering directory `/home/vmetal/libgraph-1.0.2′
    Makefile:934: warning: overriding commands for target `libgraph.pc’
    Makefile:409: warning: ignoring old commands for target `libgraph.pc’
    Making install in doc
    make[2]: Entering directory `/home/vmetal/libgraph-1.0.2/doc’
    Making install in man
    make[3]: Entering directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[4]: Entering directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[4]: Nothing to be done for `install-exec-am’.
    test -z “/usr/local/man/man1″ || mkdir -p — . “/usr/local/man/man1″
    mkdir: cannot create directory `/usr/local/man/man1′: Permission denied
    make[4]: *** [install-man1] Error 1
    make[4]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[3]: *** [install-am] Error 2
    make[3]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc/man’
    make[2]: *** [install-recursive] Error 1
    make[2]: Leaving directory `/home/vmetal/libgraph-1.0.2/doc’
    make[1]: *** [install-recursive] Error 1
    make[1]: Leaving directory `/home/vmetal/libgraph-1.0.2′
    make: *** [install] Error 2
    vmetal@ubuntu:~/libgraph-1.0.2$

  14. juggernaut says:

    ohk…my bad.. “sudo make install” workd.. :)

    bt i cant make kbhit() work

    fan.cpp: In function ‘int main()’:
    fan.cpp:113: error: ‘kbhit’ was not declared in this scope

    plz animesh… madat karo(Please help)…

  15. juggernaut says:

    and forgive me… but do u knw nethin bout

    fan.o: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) – (long) (dpy->request)) <= 0)' failed.

    actually this was the prob on my prev ubuntu version too….this ws partially the reason tht i switchd to lucid… ill b more than grateful.thnx
    :)

  16. juggernaut says:

    does “kbhit()” usage has nething to do wid….

    libsdl1.2debian-arts
    libartsc0-dev

    coz i cudnt find em thru synaptic.n proceeded without installing them.

  17. juggernaut says:

    i beg…plz help..i need this for my pojct!!

  18. juggernaut says:

    fan.o: ../../src/xcb_io.c:249: process_responses: Assertion `(((long) (dpy->last_request_read) – (long) (dpy->request)) <= 0)' failed. this is so shitty!!! im only running a cpp (also happens wid .c version) program… its a rotating fan.wid loads of pixel mapping but this error comes up evrytime and i can exit form the SDL!!

  19. Sreevisakh says:

    I have press ctrl+c on the terminal window for my graphics program to run.. How to do it automatically

  20. aitor sanchez says:

    I have 2 questions:

    1.- Include statement without the library name (stdio, stdlib,…)???
    2.- KHbit ? What’s it?

  21. ismail says:

    i have some prblem with trhis program it means which i want to run the programm so (BGI)is not supporting let me know the reasion?

  22. Amit says:

    the link to download libgraph-1.0.1 is not working, can you check and provide some link to download the package

  23. hemachandran says:

    instead of all these. we could just download and install dosbox from synaptic software center and open the TurboC and use the graphics.h library… it works.

  24. Bittu Kurian Babu says:

    thankyou for your help..
    bt i cannot use kbhit in graphics.would u please give me the solutiion.i have an error like this
    ‘kbhit’ was not declared in this scope

  25. rikin desai says:

    does this work on mac too?

  26. Arefin sami says:

    good job !!!

  27. Ashirbad says:

    turbo c is bullshit. just try using gcc. its the best and most powerful. u just cant copy paste any program, u have to modify it, it then becomes more powerful program.

  28. deepu says:

    through following error

    configure: error: *** SDL version 1.2.0 not found!

  29. rohan says:

    well sir its not working properly from the first command..

  30. pratik says:

    I installed all the packages successfully. I’m using ubuntu 11.04
    But when I ran and compiled a sample program, it gave the following message:

    Unable to init SDL: Unable to open a console terminal

    Please advise!

    This is my code:
    #include
    #include
    int main()
    {
    int gd= DETECT,gm;
    initgraph(&gd,&gm,NULL);
    line(50,50,200,200);
    getch();
    closegraph();
    return 0;
    }

  31. Bharanidharan says:

    Thank You working well
    And I’d the solution to execute kbhit() too,
    Thanks a lot:)

  32. Bharanidharan says:

    The thing is the following error is occuring,
    In file included from /usr/local/include/graphics.h:31:0,
    from test1.c:2,
    from main.c:3:
    /usr/include/unistd.h:441:21: error: conflicting types for ‘sleep’
    test0.c:9:3: note: previous implicit declaration of ‘sleep’ was here

  33. saurabh says:

    make
    make: *** No targets specified and no makefile found. Stop.

  34. Siddarth says:

    I am using UBUNTU 10.10

    The libgraph got installed successfully, the program also compiles successfully but when try to execute the program, the graphical window just flickers and goes away, not giving me the time to analyze the output, where i am going wrong please help.:(

  35. abodnya says:

    Hey, people! If u want to use simple graphic for studing, better use svgalib and vgaga. They’re better than this “pornography” with graphics.h and SDL.

    http://linux.die.net/man/7/svgalib
    http://linux.die.net/man/7/vgagl

    ALSO (THAT’S INTERESTING)
    If you’re also studing Pascal and u are using Free Pascal, when u can use simple graphic in pascal, u use svgalib. I mean, graphics in Free Pascal (FPC) also consist of svgalib and vgagl. That means, you should better use SVGAlib and VGAGL than this graphics.h

  36. srichakradhar says:

    # gcc rectangle.c
    /tmp/ccRtglXb.o: In function `main’:
    rectangle.c:(.text+0x2a): undefined reference to `initgraph’
    rectangle.c:(.text+0×39): undefined reference to `moveto’
    rectangle.c:(.text+0×52): undefined reference to `rectangle’
    rectangle.c:(.text+0x5c): undefined reference to `kbhit’
    rectangle.c:(.text+0×65): undefined reference to `closegraph’
    collect2: ld returned 1 exit status

    I’ve followed every instruction. What is the problem?
    ‘make install’ gave me the same thing as “Juggernaut” post!

  37. sakthi says:

    what is the header file we have to use…
    am getting error like

    In function `main’:
    graphics.c:(.text+0×31): undefined reference to `initgraph’
    graphics.c:(.text+0×45): undefined reference to `moveto’
    graphics.c:(.text+0×69): undefined reference to `rectangle’
    graphics.c:(.text+0x6f): undefined reference to `kbhit()’
    graphics.c:(.text+0x7b): undefined reference to `closegraph’
    collect2: ld returned 1 exit statu

  38. Manuel Nájar Joa says:

    Animesh first of all thanks for your effort to give us (Who are needing a graphics software) at least a indicatio on how to solve the lack of this kind of facility.
    but i should ask for khabi(), because several there have asked by khbit(), I have not used your indications but I have seen the several times questions about this part Khbit(). This lack is stoping me to try your proposal of any way.

    thanks for your attention to this notes Manuel and thanks for your nice work and for let me know about this alternatives.,

  39. sana says:

    i have just installed ubuntu 12.04 lts.
    and i require graphics programming..!!
    i tired following all the above steps but faced some problems…

    Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    Note, selecting ‘libasound2-dev’ instead of ‘libasound-dev’
    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

    and after extracting the libgraph file..wen i type ./configure..
    it says no such file or directory found!!

    please help….
    thankyou in advance..!!

  40. sana says:

    the ./configure is workng now..
    bt when i type sudo make , it gives the following error :

    $ sudo make
    make: *** No targets specified and no makefile found. Stop.

    please help as soon as possible..!!

  41. Asi says:

    setviewport() is not working and “Assertion `!xcb_xlib_threads_sequence_lost’ failed.”

    Pixel request out of range!! (1810,-3102)

    I m Getting These Error Can Any one Help me To solve these????? :/

  42. Asi says:

    screen is aborting after few second on executing the program….