2D Graphics Using C in Linux [ Graphics.h in Linux ]

Most of technical people are trying to migrate from windows to Linux now a days , but they are facing lot of problems with Linux . they do not even know the forums and blogs of Linux which would be useful for finding answer to their query. The main problem of open source is choosing right one for user need. Because so many open source softwares are available for a single thing. Surely user need some supportive forum or website to find that one . open Peta is also doing that job. The idea of this  post coming to my mind from my college friend . One of my colleague asked me about computer graphics program in Linux using C programming language. the questions is

How to draw line, circle , or 2D graphics in Linux Using C or C++?

I was searching Internet and found some softwares for developing graphics applications in Linux. that softwares are listed here

  1. GTK+   – Gnome Tool Kit
  2. QT  -  The X toolkit
  3. SVGALIB   [ #include<vgagl.h> ]
  4. libgraph [ #include<graphics.h> ]

GTK and QT are simple and used for high level Graphical User Interface [ GUI ] development. SVGALIB and libgraph is used for 2D graphics in Linux .  the syntax and functions are some what different for beginners  [specially the user from windows ] but most of users familiar with windows graphics.h header file in C and C++ , so we can move for libgraph which is exact one for windows graphics user . libgraph is an implementation of the Turbo C graphics API (graphics.h) on GNU/Linux using SDL [ Simple Direct Media Player ]. The library requires SDL for primitive graphics and SDL. First you need to install the following dependency packages using synaptic package manager in Ubuntu Linux to develop the graphics applications properly.

  • SDL Library
Installing SDL Library

Installing SDL Library

  • GUILE – GNU’s Ubiquitous Intelligent Language for Extension

GUILE Installation

GUILE Installation

  • After installing these two packages , download the libgraph and install in your system

#sudo su

#tar -xzvf libgraph-1.0.2.tar.gz

#cd libgraph-1.0.2

#make

# make install

For better understanding , watch this video given here ….


Developing Graphics Application

1. Open your favorite text editor and type the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdio.h"
#include "graphics.h"
int main()
{
int gd=DETECT, gm=VGAMAX;
initgraph(&amp;gd,&amp;gm, 0);
moveto(0, 0);
setcolor(4);
rectangle(50,50,500,200);
while(!kbhit());
closegraph();
return 0;
}

2. Now compile the program with lgraph library

#gcc line.c -lgraph -o output

#./output

If you get any error in this line like

./output: error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory

Do the following

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

3. Now one new window will open , there you will get the output Rectangle in Red color.

Output Window

Output Window

The advantage of libgraph is very easy to remember the syntax  [ all functions are same as turbo c graphics.h ] . still if you are facing any problem to develop graphics applications using this , send your problems through comment page of this post.

36 Responses to “2D Graphics Using C in Linux [ Graphics.h in Linux ]”

  1. Vlad says:

    Is a kbhit() included into libgraph-1.0.2 ?
    I have got a compiler warning “implicit declaration of function ‘kbhit()’” :)

  2. admin says:

    There is no predefined function like kbhit() in GCC , you can use manual code for kbhit() functionality. [ i forgot to mention in post] . but you can also use getch() instead of kbhit(). Here the modified code
    #include “stdio.h”
    #include “graphics.h”
    int main()
    {
    int gd=DETECT, gm=VGAMAX;
    initgraph(&gd,&gm, 0);
    moveto(0, 0);
    setcolor(4);
    rectangle(50,50,500,200);
    getch();
    closegraph();
    return 0;
    }

  3. Vlad says:

    Thank you, your example works perfectly! But I’d like to ask you a bit more. Is it possible to adjust the getch() function to read stdin on the fly, so that it will not stop an application until a key is pressed? It works perfectly in ncurses library using following code:

    #include
    #include

    void init_ncurses(void){
    initscr();
    noecho();
    nodelay(stdscr, 1);
    keypad(stdscr,TRUE);
    }
    int KeyCheck (void){
    int key, key1 = 0;
    do{
    key = key1;
    key1 = getch();
    }while (key1 != ERR);
    return key;
    }
    int main(){
    int key;
    init_ncurses();/* Start curses mode */
    do{
    key = KeyCheck();
    if(key)
    printw(“%d\n”,key);
    //do_step(); //move objects on the screen
    }while (key != KEY_BACKSPACE);
    endwin();/* End curses mode */
    return 0;
    }

    Is it possible in graphics.h ?

  4. thanks for this useful post.

    i am using DevC++ in ubuntu-karmic using wine only to compile graphics code.

    i hope, now i can code graphics programs in Code::Blocks

  5. unnikrisnan c says:

    kbhit will work when u take the code for kbhit() and its dependent files in a header file and include it in your program

  6. Rodolfo says:

    You mention that it works with the UBUNTU distributions of LINUX. Will it work with RedHat #8 distribution?

  7. admin says:

    Yes , it will work in all Linux distributions including redhat , You can use either manual way or yum to install it.
    My suggestion is , you can install the following package from source
    1. Guile – http://ftp.gnu.org/pub/gnu/guile/guile-1.8.7.tar.gz
    2. libgraph – http://ftp.twaren.net/Unix/NonGNU/libgraph/libgraph-1.0.2.tar.gz

    If you get any error while installing send the details with snap shots . . .

  8. anurag says:

    when i used this program i am getting this error,
    can you help, how to include graphics.h ??

    gcc rectangle.c -lgraph -o output
    rectangle.c:2:22: graphics.h: No such file or directory
    rectangle.c: In function `main’:
    rectangle.c:5: `DETECT’ undeclared (first use in this function)
    rectangle.c:5: (Each undeclared identifier is reported only once
    rectangle.c:5: for each function it appears in.)
    rectangle.c:5: `VGAMAX’ undeclared (first use in this function)
    rectangle.c:6: `amp’ undeclared (first use in this function)
    rectangle.c:6: parse error before ‘;’ token
    rectangle.c:6: parse error before ‘)’ token

    reply soon !!

  9. admin says:

    Install all necessary packages [libgraph] read this article fully . . .

  10. k.vijayakumar says:

    all the posts are really suprb. i too want to swich from win to ubuntu. once if i found all the tools which i have worked in win surely i will swich to ubuntu in full fledged manner. kalakunga…vaalthukkal

  11. admin says:

    Thanks for Your wishes

  12. UTKARSH says:

    nu.c:1:10: error: #include expects “FILENAME” or
    nu.c:2:10: error: #include expects “FILENAME” or
    nu.c: In function ‘main’:
    nu.c:5: error: ‘DETECT’ undeclared (first use in this function)
    nu.c:5: error: (Each undeclared identifier is reported only once
    nu.c:5: error: for each function it appears in.)
    nu.c:5: error: ‘VGAMAX’ undeclared (first use in this function)

    I did the same…but i dunno why there is error.
    Should I run synaptic manager again?????
    I think there no need.
    cause it didnt show any error while installing the packages.
    F1 F1

  13. UTKARSH says:

    plz help ….

  14. admin says:

    check the double quote in include statement , if you copied the same program from this page , then you manually type the double quote ….

  15. admin says:

    Try these steps
    #cp /usr/local/lib/libgraph.* /usr/lib
    #gcc line.c -lgraph -o output
    #./output

  16. UTKARSH says:

    i used < instead of " ..and copied the file by #cp /us#cp /usr/local/lib/libgraph.* /usr/lib …..
    IT WORKED!!!!
    thanks…

  17. UTKARSH says:

    can you please suggest some good tutorials/ebooks/etc for libgraph and mainly SVGLIB

  18. UTKARSH says:

    I want to install these packages on laptop.And I work offline on laptop..
    So please tell me how to install “”"dependency packages”"” using synaptic package manager in Ubuntu

  19. UTKARSH says:

    PLZ HELP AT TUTORIALS OF SVGALIB

  20. admin says:

    You can install the package “APTonCD” in your desktop [Where you installed libgraph], then simply make one image file using this softwares. after that
    you can install the deb packages in other system without Internet.

  21. nanderv says:

    Hi..
    My error I get is about 30 times longer then the program itself?
    Can you help me? I get this error all the time. My compiler is up to date.

    In file included from line.c:1:
    /usr/include/stdio.h:34:21: error: stddef.h: No such file or directory
    In file included from /usr/include/stdio.h:75,
    from line.c:1:
    /usr/include/libio.h:53:21: error: stdarg.h: No such file or directory
    In file included from /usr/include/stdio.h:75,
    from line.c:1:
    /usr/include/libio.h:332: error: expected specifier-qualifier-list before ‘size_t’
    /usr/include/libio.h:364: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/libio.h:373: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/libio.h:489: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/libio.h:491: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/libio.h:493: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’
    In file included from line.c:1:
    /usr/include/stdio.h:296: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:302: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:314: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:321: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:349: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:354: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:357: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:363: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:365: error: format string argument not a string type
    /usr/include/stdio.h:367: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:368: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:369: error: format string argument not a string type
    /usr/include/stdio.h:395: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:454: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:461: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:466: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:476: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:481: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:484: error: expected declaration specifiers or ‘…’ before ‘__gnuc_va_list’
    /usr/include/stdio.h:639: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:642: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:652: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdio.h:682: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
    /usr/include/stdio.h:688: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
    /usr/include/stdio.h:710: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread_unlocked’
    /usr/include/stdio.h:712: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite_unlocked’
    In file included from /usr/include/SDL/SDL_stdinc.h:38,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/stdlib.h:140: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘__ctype_get_mb_cur_max’
    /usr/include/stdlib.h:337: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:367: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:369: error: nonnull argument with out-of-range operand number (argument 1, operand 4)
    /usr/include/stdlib.h:471: error: expected ‘)’ before ‘__size’
    /usr/include/stdlib.h:473: error: expected ‘)’ before ‘__nmemb’
    /usr/include/stdlib.h:485: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/include/stdlib.h:497,
    from /usr/include/SDL/SDL_stdinc.h:38,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/alloca.h:33: error: expected ‘)’ before ‘__size’
    In file included from /usr/include/SDL/SDL_stdinc.h:38,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/stdlib.h:502: error: expected ‘)’ before ‘__size’
    /usr/include/stdlib.h:507: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:507: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:705: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:705: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:706: error: nonnull argument with out-of-range operand number (argument 1, operand 5)
    /usr/include/stdlib.h:710: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:710: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:711: error: nonnull argument with out-of-range operand number (argument 1, operand 4)
    /usr/include/stdlib.h:788: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:791: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:795: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:799: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:808: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/stdlib.h:811: error: expected ‘)’ before ‘*’ token
    /usr/include/stdlib.h:815: error: expected declaration specifiers or ‘…’ before ‘wchar_t’
    /usr/include/stdlib.h:819: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘mbstowcs’
    /usr/include/stdlib.h:822: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘wcstombs’
    In file included from /usr/include/SDL/SDL_stdinc.h:58,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/string.h:44: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:48: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:57: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:64: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:67: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:94: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:131: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:139: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:145: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:152: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strxfrm’
    In file included from /usr/include/SDL/SDL_stdinc.h:58,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/string.h:167: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strxfrm_l’
    /usr/include/string.h:181: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:282: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strcspn’
    /usr/include/string.h:286: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strspn’
    /usr/include/string.h:397: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strlen’
    /usr/include/string.h:404: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strnlen’
    /usr/include/string.h:425: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:449: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:453: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:457: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:460: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:538: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:575: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/string.h:578: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/include/SDL/SDL_stdinc.h:72,
    from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/iconv.h:43: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘iconv’
    In file included from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/SDL/SDL_stdinc.h:384: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘SDL_strlcpy’
    /usr/include/SDL/SDL_stdinc.h:390: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘SDL_strlcat’
    /usr/include/SDL/SDL_stdinc.h:582: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘SDL_iconv’
    In file included from /usr/include/SDL/SDL_main.h:26,
    from /usr/include/SDL/SDL.h:28,
    from /usr/local/include/graphics.h:30,
    from line.c:2:
    /usr/include/SDL/SDL_stdinc.h:586: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/local/include/graphics.h:31,
    from line.c:2:
    /usr/include/unistd.h:329: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:335: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:479: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/local/include/graphics.h:31,
    from line.c:2:
    /usr/include/unistd.h:582: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘confstr’
    /usr/include/unistd.h:754: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:790: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:801: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:837: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/local/include/graphics.h:31,
    from line.c:2:
    /usr/include/unistd.h:859: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:866: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:877: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:879: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:897: error: expected declaration specifiers or ‘…’ before ‘size_t’
    /usr/include/unistd.h:898: error: expected declaration specifiers or ‘…’ before ‘size_t’
    In file included from /usr/include/signal.h:356,
    from /usr/local/include/graphics.h:33,
    from line.c:2:
    /usr/include/bits/sigstack.h:54: error: expected specifier-qualifier-list before ‘size_t’
    In file included from /usr/local/include/graphics.h:36,
    from line.c:2:
    /usr/include/time.h:199: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strftime’
    /usr/include/time.h:217: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘strftime_l’
    line.c: In function ‘main’:
    line.c:6: error: ‘amp’ undeclared (first use in this function)
    line.c:6: error: (Each undeclared identifier is reported only once
    line.c:6: error: for each function it appears in.)
    line.c:6: error: expected ‘)’ before ‘;’ token
    line.c:6: error: too few arguments to function ‘initgraph’

    If I use Code::Blocks, the error get’s shorter, but doesn’t vanish:
    obj/Debug/main.o||In function `main’:|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|6|undefined reference to `initgraph’|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|7|undefined reference to `moveto’|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|8|undefined reference to `setcolor’|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|9|undefined reference to `rectangle’|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|10|undefined reference to `grgetch’|
    /home/administrator/Dropbox/Desktop/irrtest/test/main.cpp|11|undefined reference to `closegraph’|
    ||=== Build finished: 6 errors, 0 warnings ===|
    that seems to be the default linker problem.

  22. admin says:

    refer this page http://gcc.gnu.org/ml/gcc-help/2009-07/msg00009.html

    Send your program if you could not find the solution

  23. nk1 says:

    Hey

    Can i use this library for pixel plotting………seeing that c does not provide steps to go to a set pixel

    Please reply fast…..have to make a project

    Thanxx a lot!!!

  24. admin says:

    Use putpixel(x,y,color) ,that is the function for drawing pixel in libgraph, see the documentation for list of functions

    Download libgraph source and goto doc folder ….
    http://ftp.twaren.net/Unix/NonGNU/libgraph/

  25. Ric Colasanti says:

    THanks this was the most useful version of installing libgraph that I have found. Can you give any hints as to good documentation for graphics.h, other than rooting out old turbo C books (which I am doing)
    Thank you

  26. admin says:

    There is no proper documentation for libgraph , you can refer the API page for syntax and description which comes with the source code . . .

  27. Voldemort says:

    hi,
    i’ve installed it in ubuntu 9.10
    It doesn’t any errors.
    However when i compile in Net-bean,
    uild/Debug/GNU-Linux-x86/main.o: In function `main’:
    /main.cpp:8: undefined reference to `initgraph’
    /main.cpp:9: undefined reference to `moveto’
    /main.cpp:10: undefined reference to `rectangle’
    /main.cpp:12: undefined reference to `closegraph’

    What i have to do to fix it ?
    Thanks

  28. Voldemort says:

    Ps: sorry for 2 post. I’ve installed all packages

  29. admin says:

    #include “stdio.h”
    #include “graphics.h”

    int main()
    {
    int gd=DETECT, gm=VGAMAX;
    initgraph(&gd,&gm, 0);
    moveto(0, 0);
    setcolor(4);
    rectangle(50,50,500,200);
    getch();
    closegraph();
    return 0;
    }

    Right click on C Project Folder in Netbeans Select “Set Configuration” => “Customise” Option

    In That Window add -lgraph in command line parameter options

    Then program will run properly …

  30. admin says:

    watch this video for Graphics.h using Netbeans
    http://www.youtube.com/watch?v=wvgc3uBiYjw

  31. Deepak Jain says:

    After installation of SDL library and GUILE , I have downloaded the libgraph-1.0.2.tar.gz and extract it. But when I do configure as shown in video……
    I get something else….

    root@deepak-laptop:/home/deepak/Documents/libgraph-1.0.2# ./configure
    checking build system type… i686-pc-linux-gnu
    checking host system type… i686-pc-linux-gnu
    checking target system type… i686-pc-linux-gnu
    checking for a BSD-compatible install… /usr/bin/install -c
    checking whether build environment is sane… yes
    checking for gawk… no
    checking for mawk… mawk
    checking whether make sets $(MAKE)… yes
    checking whether to enable maintainer-specific portions of Makefiles… no
    checking for gcc… gcc
    checking for C compiler default output file name… a.out
    checking whether the C compiler works… yes
    checking whether we are cross compiling… no
    checking for suffix of executables…
    checking for suffix of object files… o
    checking whether we are using the GNU C compiler… yes
    checking whether gcc accepts -g… yes
    checking for gcc option to accept ANSI C… none needed
    checking for style of include used by make… GNU
    checking dependency style of gcc… gcc3
    checking how to run the C preprocessor… gcc -E
    checking for a BSD-compatible install… /usr/bin/install -c
    checking whether ln -s works… yes
    checking whether make sets $(MAKE)… (cached) yes
    checking for a sed that does not truncate output… /bin/sed
    checking for egrep… grep -E
    checking for ld used by gcc… /usr/bin/ld
    checking if the linker (/usr/bin/ld) is GNU ld… yes
    checking for /usr/bin/ld option to reload object files… -r
    checking for BSD-compatible nm… /usr/bin/nm -B
    checking how to recognise dependent libraries… pass_all
    checking for ANSI C header files… yes
    checking for sys/types.h… yes
    checking for sys/stat.h… yes
    checking for stdlib.h… yes
    checking for string.h… yes
    checking for memory.h… yes
    checking for strings.h… yes
    checking for inttypes.h… yes
    checking for stdint.h… yes
    checking for unistd.h… yes
    checking dlfcn.h usability… yes
    checking dlfcn.h presence… yes
    checking for dlfcn.h… yes
    checking for g++… no
    checking for c++… no
    checking for gpp… no
    checking for aCC… no
    checking for CC… no
    checking for cxx… no
    checking for cc++… no
    checking for cl… no
    checking for FCC… no
    checking for KCC… no
    checking for RCC… no
    checking for xlC_r… no
    checking for xlC… no
    checking whether we are using the GNU C++ compiler… no
    checking whether g++ accepts -g… no
    checking dependency style of g++… none
    checking how to run the C++ preprocessor… /lib/cpp
    configure: error: C++ preprocessor “/lib/cpp” fails sanity check
    See `config.log’ for more details.
    root@deepak-laptop:/home/deepak/Documents/libgraph-1.0.2#

    that’s why when I compile the code:-

    #include “stdio.h”
    #include “graphics.h”
    int main()
    {
    int gd=DETECT, gm=VGAMAX;
    initgraph(&gd,&gm, 0);
    moveto(0, 0);
    setcolor(4);
    rectangle(50,50,500,200);
    while(!kbhit());
    closegraph();
    return 0;
    }

    It gives me error..

    check.c:2:22: error: graphics.h: No such file or directory
    check.c: In function ‘main’:
    check.c:5: error: ‘DETECT’ undeclared (first use in this function)
    check.c:5: error: (Each undeclared identifier is reported only once
    check.c:5: error: for each function it appears in.)
    check.c:5: error: ‘VGAMAX’ undeclared (first use in this function)
    check.c:6: error: ‘amp’ undeclared (first use in this function)
    check.c:6: error: expected ‘)’ before ‘;’ token

    What should I do………..?
    plz reply….

  32. admin says:

    install GCC in your system
    #sudo apt-get install build-essential

    then try to install libgraph …

  33. Ranjib says:

    Hi guys
    Can you please help me out? I got a graphics program in C over the net. I made no change in the program, simply did copy<paste. I have installed libgraph-1.0.1. But when I am running the code I am getting the following list of run time errors for a graphics program in C although no compilation error.

    *** glibc detected *** ./gr: malloc(): memory corruption (fast): 0x092f1c00 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6(+0x6b591)[0xe6f591]
    /lib/tls/i686/cmov/libc.so.6(+0x6e710)[0xe72710]
    /lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x5c)[0xe73f9c]
    /usr/lib/libX11.so.6(_XReply+0xa2)[0x3f7f52]
    /usr/lib/libX11.so.6(XSync+0×67)[0x3eb847]
    /usr/lib/libSDL-1.2.so.0(+0x36b25)[0x163b25]
    /usr/lib/libSDL-1.2.so.0(+0x3bb5f)[0x168b5f]
    /usr/lib/libSDL-1.2.so.0(SDL_UpdateRects+0x1de)[0x15af2e]
    /usr/lib/libSDL-1.2.so.0(SDL_UpdateRect+0×74)[0x15b074]
    /usr/lib/libgraph.so.1(safe_update+0x8c)[0x79e9a7]
    /usr/lib/libgraph.so.1(circle+0×152)[0x7a10dd]
    ./gr[0x8048d42]
    ./gr[0x8048978]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xe1abd6]
    ./gr[0x80488a1]
    ======= Memory map: ========
    00110000-0011b000 r-xp 00000000 08:05 10965407 /usr/lib/libSDL_image-1.2.so.0.8.2
    0011b000-0011c000 r–p 0000a000 08:05 10965407 /usr/lib/libSDL_image-1.2.so.0.8.2
    0011c000-0011d000 rw-p 0000b000 08:05 10965407 /usr/lib/libSDL_image-1.2.so.0.8.2
    0011d000-0012d000 rw-p 00000000 00:00 0
    0012d000-00190000 r-xp 00000000 08:05 10961866 /usr/lib/libSDL-1.2.so.0.11.3
    00190000-00191000 r–p 00063000 08:05 10961866 /usr/lib/libSDL-1.2.so.0.11.3
    00191000-00192000 rw-p 00064000 08:05 10961866 /usr/lib/libSDL-1.2.so.0.11.3
    00192000-001bc000 rw-p 00000000 00:00 0
    001bc000-001bf000 r-xp 00000000 08:05 10964371 /usr/lib/libpulse-simple.so.0.0.3
    001bf000-001c0000 r–p 00002000 08:05 10964371 /usr/lib/libpulse-simple.so.0.0.3
    001c0000-001c1000 rw-p 00003000 08:05 10964371 /usr/lib/libpulse-simple.so.0.0.3
    001c1000-001c9000 r-xp 00000000 08:05 10963859 /usr/lib/libfusion-1.2.so.0.8.0
    001c9000-001ca000 r–p 00007000 08:05 10963859 /usr/lib/libfusion-1.2.so.0.8.0
    001ca000-001cb000 rw-p 00008000 08:05 10963859 /usr/lib/libfusion-1.2.so.0.8.0
    001cb000-001d0000 r-xp 00000000 08:05 10964279 /usr/lib/libogg.so.0.6.0
    001d0000-001d1000 r–p 00004000 08:05 10964279 /usr/lib/libogg.so.0.6.0
    001d1000-001d2000 rw-p 00005000 08:05 10964279 /usr/lib/libogg.so.0.6.0
    001d2000-001e5000 r-xp 00000000 08:05 11903174 /lib/libz.so.1.2.3.3
    001e5000-001e6000 r–p 00012000 08:05 11903174 /lib/libz.so.1.2.3.3
    001e6000-001e7000 rw-p 00013000 08:05 11903174 /lib/libz.so.1.2.3.3
    001e7000-00227000 r-xp 00000000 08:05 10964373 /usr/lib/libpulse.so.0.12.2
    00227000-00228000 r–p 00040000 08:05 10964373 /usr/lib/libpulse.so.0.12.2
    00228000-00229000 rw-p 00041000 08:05 10964373 /usr/lib/libpulse.so.0.12.2
    00229000-0023e000 r-xp 00000000 08:05 10963520 /usr/lib/libICE.so.6.3.0
    0023e000-0023f000 r–p 00014000 08:05 10963520 /usr/lib/libICE.so.6.3.0
    0023f000-00240000 rw-p 00015000 08:05 10963520 /usr/lib/libICE.so.6.3.0
    00240000-00242000 rw-p 00000000 00:00 0
    00242000-0024e000 r-xp 00000000 08:05 10963576 /usr/lib/libXi.so.6.1.0
    0024e000-0024f000 r–p 0000c000 08:05 10963576 /usr/lib/libXi.so.6.1.0
    0024f000-00250000 rw-p 0000d000 08:05 10963576 /usr/lib/libXi.so.6.1.0
    00250000-00263000 r-xp 00000000 08:05 11920933 /lib/tls/i686/cmov/libnsl-2.11.1.so
    00263000-00264000 r–p 00012000 08:05 11920933 /lib/tls/i686/cmov/libnsl-2.11.1.so
    00264000-00265000 rw-p 00013000 08:05 11920933 /lib/tls/i686/cmov/libnsl-2.11.1.so
    00265000-00267000 rw-p 00000000 00:00 0
    00267000-0026b000 r-xp 00000000 08:05 10963566 /usr/lib/libXdmcp.so.6.0.0
    0026b000-0026c000 r–p 00003000 08:05 10963566 /usr/lib/libXdmcp.so.6.0.0
    0026c000-0026d000 rw-p 00004000 08:05 10963566 /usr/lib/libXdmcp.so.6.0.0
    00272000-002bb000 r-xp 00000000 08:05 10964374 /usr/lib/libpulsecommon-0.9.21.so
    002bb000-002bc000 r–p 00048000 08:05 10964374 /usr/lib/libpulsecommon-0.9.21.so
    002bc000-002bd000 rw-p 00049000 08:05 10964374 /usr/lib/libpulsecommon-0.9.21.so
    002bd000-0031e000 r-xp 00000000 08:05 10964433 /usr/lib/libsndfile.so.1.0.21
    0031e000-0031f000 —p 00061000 08:05 10964433 /usr/lib/libsndfile.so.1.0.21
    0031f000-00320000 r–p 00061000 08:05 10964433 /usr/lib/libsndfile.so.1.0.21
    00320000-00321000 rw-p 00062000 08:05 10964433 /usr/lib/libsndfile.so.1.0.21
    00321000-00325000 rw-p 00000000 00:00 0
    00325000-00370000 r-xp 00000000 08:05 10963505 /usr/lib/libFLAC.so.8.2.0
    00370000-00371000 r–p 0004a000 08:05 10963505 /usr/lib/libFLAC.so.8.2.0
    00371000-00372000 rw-p 0004b000 08:05 10963505 /usr/lib/libFLAC.so.8.2.0
    00372000-00399000 r-xp 00000000 08:05 10964521 /usr/lib/libvorbis.so.0.4.3
    00399000-0039a000 r–p 00026000 08:05 10964521 /usr/lib/libvorbis.so.0.4.3
    0039a000-0039b000 rw-p 00027000 08:05 10964521 /usr/lib/libvorbis.so.0.4.3
    0039b000-003a3000 r-xp 00000000 08:05 10963562 /usr/lib/libXcursor.so.1.0.2
    003a3000-003a4000 r–p 00007000 08:05 10963562 /usr/lib/libXcursor.so.1.0.2
    003a4000-003a5000 rw-p 00008000 08:05 10963562 /usr/lib/libXcursor.so.1.0.2
    003a5000-003ad000 r-xp 00000000 08:05 10963590 /usr/lib/libXrender.so.1.3.0
    003ad000-003ae000 r–p 00007000 08:05 10963590 /usr/lib/libXrender.so.1.3.0
    003ae000-003af000 rw-p 00008000 08:05 10963590 /usr/lib/libXrender.so.1.3.0
    003b6000-004cf000 r-xp 00000000 08:05 10963551 /usr/lib/libX11.so.6.3.0
    004cf000-004d0000 r–p 00118000 08:05 10963551 /usr/lib/libX11.so.6.3.0
    004d0000-004d2000 rw-p 00119000 08:05 10963551 /usr/lib/libX11.so.6.3.0
    004d2000-004d3000 rw-p 00000000 00:00 0
    004d3000-004f6000 r-xp 00000000 08:05 11903110 /lib/libpng12.so.0.42.0
    004f6000-004f7000 r–p 00022000 08:05 11903110 /lib/libpng12.so.0.42.0
    004f7000-004f8000 rw-p 00023000 08:05 11903110 /lib/libpng12.so.0.42.0
    004f8000-00515000 r-xp 00000000 08:05 11903059 /lib/libgcc_s.so.1
    00515000-00516000 r–p 0001c000 08:05 11903059 /lib/libgcc_s.so.1
    00516000-00517000 rw-p 0001d000 08:05 11903059 /lib/libgcc_s.so.1
    00575000-00599000 r-xp 00000000 08:05 11920929 /lib/tls/i686/cmov/libm-2.11.1.so
    00599000-0059a000 r–p 00023000 08:05 11920929 /lib/tls/i686/cmov/libm-2.11.1.so
    0059a000-0059b000 rw-p 00024000 08:05 11920929 /lib/tls/i686/cmov/libm-2.11.1.so
    0066c000-00670000 r-xp 00000000 08:05 10963596 /usr/lib/libXtst.so.6.1.0
    00670000-00671000 r–p 00003000 08:05 10963596 /usr/lib/libXtst.so.6.1.0
    00671000-00672000 rw-p 00004000 08:05 10963596 /usr/lib/libXtst.so.6.1.0
    00673000-00675000 r-xp 00000000 08:05 10963555 /usr/lib/libXau.so.6.0.0
    00675000-00676000 r–p 00001000 08:05 10963555 /usr/lib/libXau.so.6.0.0
    00676000-00677000 rw-p 00002000 08:05 10963555 /usr/lib/libXau.so.6.0.0
    0067c000-0068a000 r-xp 00000000 08:05 10963568 /usr/lib/libXext.so.6.4.0
    0068a000-0068b000 r–p 0000d000 08:05 10963568 /usr/lib/libXext.so.6.4.0
    0068b000-0068c000 rw-p 0000e000 08:05 10963568 /usr/lib/libXext.so.6.4.0
    00723000-0073e000 r-xp 00000000 08:05 11903277 /lib/ld-2.11.1.so
    0073e000-0073f000 r–p 0001a000 08:05 11903277 /lib/ld-2.11.1.soAborted

    My source code is here:

    #include
    //#include
    #include
    #include
    #include
    //#include
    int gph[20][20];
    int nd;
    struct cod
    {
    int x,y;
    };
    void RandGraph(void);
    void Disp(void);
    void PlotGraph(void);
    void main()
    {
    int gd=0,gm=0;
    initgraph(&gm,&gd,”e:\\TC\\bgi”);
    RandGraph();
    Disp();
    PlotGraph();
    getch();
    }
    void RandGraph()
    {
    int i,j,k;
    time_t t;
    srand((unsigned) time(&t));
    printf(“\nEnter number of nodes :”);
    scanf(“%d”,&nd);
    for(i=0;i<=nd-1;i++)
    {
    for(j=i;j=55)
    {
    gph[i][j]=1;
    gph[j][i]=1;
    }
    else
    {
    gph[i][j]=0;
    gph[j][i]=0;
    }
    }
    }
    }
    }
    void Disp()
    {
    int i,j;
    printf(“\nThe Random Graph is :\n\n”);
    for(i=0;i<=nd-1;i++)
    {
    for(j=0;j<=nd-1;j++)
    printf("%d ",gph[i][j]);
    printf("\n");
    }
    }
    void PlotGraph()
    {
    int k=0,i,j,rmx,rmy,p=410,q=100,rw,b=10,a=30;
    struct cod *xy,*pq;
    char ch[4],h=97;
    xy=(struct cod *)malloc(sizeof(struct cod)*nd);
    pq=xy;
    if(nd%3 == 0)
    rw=nd/3-1;
    else
    rw=nd/3;
    for(i=0;i<=rw;i++)
    {
    for(j=0;jx=p;
    xy->y=q+b;
    xy++;
    k++;
    b=-b;
    p=p+50;
    }
    q=q+50;
    p=410+a;
    a=-a;
    }
    xy=pq;
    setcolor(GREEN);
    k=0;
    for(i=0;i<=rw;i++)
    {
    if(i!=rw)
    {
    for(j=0;jx,xy->y,5);
    ch[0]=h;
    ch[1]=”;
    moveto(xy->x+8,xy->y-8);
    outtext(ch);
    rmx=xy->x;
    rmy=xy->y;
    xy++;
    h++;
    }
    }
    else
    {
    if(nd %3 !=0)
    {
    for(j=0;jx,xy->y,5);
    ch[0]=h;
    ch[1]=”;
    moveto(xy->x+8,xy->y-8);
    outtext(ch);
    rmx=xy->x;
    rmy=xy->y;
    xy++;
    h++;
    }
    }
    else
    {
    for(j=0;jx,xy->y,5);
    ch[0]=h;
    ch[1]=”;
    moveto(xy->x+8,xy->y-8);
    outtext(ch);
    rmx=xy->x;
    rmy=xy->y;
    xy++;
    h++;
    }
    }
    }
    }
    if(nd%3!=0)
    {
    setcolor(BLACK);
    circle(rmx,rmy,5);
    moveto(rmx+8,rmy-8);
    outtext(ch);
    }
    xy=pq;
    setcolor(RED);
    for(i=0;i<=nd-1;i++)
    {
    for(j=i;jx,(xy+i)->y,(xy+j)->x,(xy+j)->y);
    }
    }
    }

    Please help me.

    Thank you all in advance.

  34. admin says:

    Have you tried with simple graphics program???

  35. Ranjib says:

    yes, I tried with the one that’s given here. I changed a bit and it worked, then I tried this one and I got the above errors.
    Can you please help me how can I save the rectangle plotted by the first sample program given here?

Leave a Response