Saturday, October 16, 2010

How to Install Graphics Libraries and use under (Linux) gcc/g++ (graphics.h)

Here are the steps that I used.

  1. In order to include graphics.h header file, you need to install libgraph package.
  2. Search the source code for “libgraph”. Install by using commands,
    make install
  3. Sometimes, you may get error message “configure: error: *** SDL version x.x.x not found!”. You can solve this by installing “apt-get install libsdl1.2-dev”.
  4. Sometimes, you may get error message “configure: cannot find guile-config; is Guile installed?”. You can solve this by installing “sudo apt-get install guile guile-dev”.
  5. Sometimes, you may get error message “SDL_image-x-xx-x not found”. You may install by searching sourcecode “SDL_image” and following commands “./cofigure make make install”.
  6. When I tried to compile a C file, I got error message,
    vksalian@vksalian-laptop:~/Desktop$ gcc vvv.c -o vvv -lSDL -lSDL_image
    In file included from /usr/local/include/grtext.h:26,
    from /usr/local/include/graphics.h:116,
    from vvv.c:8:
    /usr/local/include/SDL/SDL_image.h:28:17: error: SDL.h: No such file or directory
    /usr/local/include/SDL/SDL_image.h:29:25: error: SDL_version.h: No such file or directory
    /usr/local/include/SDL/SDL_image.h:30:24: error: begin_code.h: No such file or directory
    /usr/local/include/SDL/SDL_image.h:134:24: error: close_code.h: No such file or directory
  7. sudo apt-get install libsdl-image1.2-dev freetype, libsdl1.2, libsdl1.2-dev, libsdl-image1.2-dev, and libsdl-image1.2.
  8. Copy header files with command “sudo cp /usr/local/lib/libgraph.* /usr/lib
  9. Then changed the arguiments to compile the C Program and the command looks like (all libraries included),
    gcc -g -Wall -I/usr/include/SDL -I/usr/local/include/SDL -I/usr/local/include -D_GNU_SOURCE=1 -D_REENTRANT -L/usr/lib -lSDL -I/usr/local/include/SDL -lSDL -lSDL_image -I/usr/local/include -lgraph -o vvv vvv.c
  10. Please note the below point.
    `sdl-config –cflags` & `sdl-config –libs` can replaced for -I/usr/include/SDL -I/usr/local/include/SDL -I/usr/local/include -D_GNU_SOURCE=1 -D_REENTRANT -L/usr/lib -lSDL -I/usr/local/include/SDL -lSDL -lSDL_image -I/usr/local/include
  11. Sample C Program I used to test this,
    void main()
    int gd,gm=VGAMAX;
    int a;
    return 0;

  1. You may use the article below (for more details).

        Graphics.h in GNU/Linux By

    Ive read and pondered upon lots of blogs and posts regarding the topics "20xx is the year of the linux desktop" , "linux is not yet ready for the desktop" etc etc.I guess the answer to all such questions is personal in nature. I mean id say "try and find out". I started using a GNU/Linux distribution about 8 months ago. Since then my aim was to do whatever stuff i was doing on XP, on GNU/Linux and maybe find stuff i couldnt do on XP. Thats how i approached/read about GNU/Linux though i later found out that, that approach isn't exactly right, as i was merely "reducing" GNU/Linux to a windows replacement, while its actually a whole another approach to computing.
    Anyways, since i joined engineering if there was one thing i didnt like at all, then it was the "Blue Screen Of Death" or the Turbo C interface. Since s3 i never used Turbo C at home. I've always preferred Visual Studio or Dev C++ over Turbo C. I just didnt like the interface. This dislike of turbo C didnt however hamper my coding functionalities in other IDE's as they always had what i wanted. After moving to GNU/Linux i was able to do the same programs and compile them using G++. But s7 is were the problem starts. Now i gotta "computer graphics" lab which makes extensive use of the "graphics.h" library. I had heard rumours earlier that it wasnt available in all IDE's and to my dismay i found out that it wasnt there in GNU/Linux either. Another problem was that since the functions it included involved direct access to the hardware merely copying the graphics.h header to the library folders wouldnt help. As always google helped. Stumbled upon the blog of a person who was in the same situation as I am and he had found a solution. The answer to my problem was a set of libraries called libgraph which wraps around the SDL API.
    His blog did contain steps to install in debian but it didnt work for me coz of minor hiccups. SO i spent sometime and finally succeeded in getting it to work on my ubuntu feisty install. I just thought i'd post it here so that google drops this in front of another seeker :)

    Step 1: Installing dependencies

    run sudo apt-get install build-essential to make sure you have installed all necessary compiler tools

    Now comes the real achy part. Open synaptic and install the following packages


    You can install them in a single go by adding all the above package names(separated by a black space) to the end of sudo apt-get install Even if it asks you to uninstall some already installed stuff, tell it to proceed. After all this is over. Download this file onto your home folder: libgraph-1.0.1

    Right click on the file u just downloaded and click "Extract here"

    open terminal and navigate into the folder u just extracted the file contents into by running cd libgraph-1.0.1
    Now run the following commands one after the another. Proceed to the next command only if the preceding command executes without any errors.

    sudo make
    make install
    If you get any errors during the "sudo make" step, paste it in the comments section of this post.

    Now technically install is over. Try writing a simple program including graphics.h. Declare the values of gd and gm and call initgraph as follows

    int gd,gm=VGAMAX; gd=DETECT;initgraph(&gd,&gm,NULL);

    and when compiling using g++ add an extra paramter -lgraph
    i.e if you are compiling a program dda.cpp in the terminal u type in

    g++ dda.cpp -o dda.o -lgraph
    And execute the program by runnung ./dda.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
    I also read somewhere that graphics.h dosen't have ANY practical use in today's world and that its only "use" now is to help students learn to apply different algorithm's they have to study in a paper called "Computer Graphics"


  1. Makefile:934: warning: overriding commands for target `libgraph.pc'
    Makefile:409: warning: ignoring old commands for target `libgraph.pc'
    if /bin/bash ./libtool --mode=compile gcc -DHAVE_CONFIG_H -I. -I. -I. -I. -g -O2 -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -DFONTDIR=\""/usr/local/share/libgraph/Font/"\" -g -O2 -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -MT guile-libgraph.lo -MD -MP -MF ".deps/guile-libgraph.Tpo" -c -o guile-libgraph.lo guile-libgraph.c; \
    then mv -f ".deps/guile-libgraph.Tpo" ".deps/guile-libgraph.Plo"; else rm -f ".deps/guile-libgraph.Tpo"; exit 1; fi
    gcc -DHAVE_CONFIG_H -I. -I. -I. -I. -g -O2 -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -DFONTDIR=\"/usr/local/share/libgraph/Font/\" -g -O2 -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -MT guile-libgraph.lo -MD -MP -MF .deps/guile-libgraph.Tpo -c guile-libgraph.c -fPIC -DPIC -o .libs/guile-libgraph.o
    guile-libgraph.c:25:22: fatal error: libguile.h: No such file or directory
    compilation terminated.
    make[2]: *** [guile-libgraph.lo] Error 1
    make[2]: Leaving directory `/home/prince/libgraph-1.0.2'
    make[1]: *** [all-recursive] Error 1
    make[1]: Leaving directory `/home/prince/libgraph-1.0.2'
    make: *** [all] Error 2

    Please tell how to solve this.

    1. Make sure you have did : sudo apt-get install guile guile-dev

      The modern ubuntu OS (14.04)has a different version of guile 2.0. So the command will be : sudo apt-get install guile-2.0-dev

      Note : The path of guile installed will be different and hence the libguile.h may not be accessible for the make command. To solve this, I just created symbolic links to those header files as below,

      sudo ln -s /usr/include/guile/2.0/libguile /usr/include/libguile
      sudo ln -s /usr/include/guile/2.0/libguile.h /usr/include/

    2. It works! I just use the 2 symbolic links and it works. Thank you Vijay Salian. Good Work.

  2. Search for "guile" under synaptic package manager and install the associated files. Also, re-check if "libgc-dev" and "guile-2.0-dev" are installed properly.

    This should solve above error.

  3. Any Sol'n for this :

    harvardboy95@ubuntu:~/Desktop/Programs$ gcc heart.c -o heart -lgraph
    harvardboy95@ubuntu:~/Desktop/Programs$ ./heart
    ./heart: error while loading shared libraries: cannot open shared object file: No such file or directory

  4. Hello Kirtan,

    Try this link :

  5. i got an error while configure
    checking for gawk... (cached) mawk
    checking target system type... i686-pc-linux-gnu
    checking for sdl-config... /usr/bin/sdl-config
    checking for SDL - version >= 1.2.0... yes
    checking SDL ==> SDL_image library... configure: error: *** SDL_image not found

    1. Try This : sudo apt-get install libsdl-image1.2-dev

  6. Makefile:781: warning: overriding commands for target `libgraph.pc'
    Makefile:333: warning: ignoring old commands for target `libgraph.pc'
    Making install in doc
    make[1]: Entering directory `/home/ajay/libgraph-1.0.1/doc'
    Making install in man
    make[2]: Entering directory `/home/ajay/libgraph-1.0.1/doc/man'
    make[3]: Entering directory `/home/ajay/libgraph-1.0.1/doc/man'
    make[3]: Nothing to be done for `install-exec-am'.
    /bin/bash ../../mkinstalldirs /usr/local/man/man1
    mkdir -p -- /usr/local/man/man1
    mkdir: cannot create directory ‘/usr/local/man/man1’: Permission denied
    make[3]: *** [install-man1] Error 1
    make[3]: Leaving directory `/home/ajay/libgraph-1.0.1/doc/man'
    make[2]: *** [install-am] Error 2
    make[2]: Leaving directory `/home/ajay/libgraph-1.0.1/doc/man'
    make[1]: *** [install-recursive] Error 1
    make[1]: Leaving directory `/home/ajay/libgraph-1.0.1/doc'
    make: *** [install-recursive] Error 1

    1. I'm getting same error. please reply asap

    2. Hi Virad,

      What I can see is you have called "make install" command. But in order to install the compiled package you need to be super user. Try "sudo make install" or just "make install" as ROOT.

  7. Hi Virad,

    What I can see is you have called "make install" command. But in order to install the compiled package you need to be super user. Try "sudo make install" or just "make install" as ROOT.

  8. graph.cpp:(.text+0x32): undefined reference to `initgraph'
    graph.cpp:(.text+0x37): undefined reference to `cleardevice'
    graph.cpp:(.text+0xf8): undefined reference to `grgetch'
    graph.cpp:(.text+0xfd): undefined reference to `closegraph'

    The above problems can be solved by " sudo apt-get install guile-2.0-dev"