How to use graphics.h in Ubuntu?

Semester 7 at college came up with a bunch of new and interesting stuff. One of them is the computer graphics lab, where we all got to do simple graphic programming in C.

Soon enough, this turned out to be a bit uneasy.
The reason was that we were forced to work in Windows, since there is no support for graphics.h in the gcc compiler in Ubuntu. To make things worse, the computers had Windows 7, and we had to install a simulator called DOS-box to run the programs properly. All this felt so messy and I just wanted to run back to the comfort of programming in Ubuntu!

Then there was some solution here. Basically, we've to install some packages and libraries which enable us to compile graphics.h programs in linux. The original article is a bit old and many packages mentioned are obsolete now. So I've written a patched up, more up-to-date version of the same.
Hope this saves you from going back to the bitter blue screen of Turbo C ;)


Click image to enlarge

Step #0
Make sure you have the basic compilers installed.
You need the build-essential package. For this, run the command:  

sudo apt-get install build-essential


Step #1
First we need to install a hand full of packages. You can simply run the following command and get it all done.   

sudo apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev libsdl1.2debian-all libart-2.0-dev libaudiofile-dev libesd0-dev libdirectfb-dev libdirectfb-extra libfreetype6-dev libxext-dev x11proto-xext-dev libfreetype6 libaa1 libaa1-dev libslang2-dev libasound2 libasound2-dev


Step #2
Now, download libgraph.

Copy the file libgraph-1.0.2.tar.gz to our home folder. Right click on the file and select Extract here.

Open a terminal and run the following commands, one by one. 

cd libgraph-1.0.2
./configure
sudo make
sudo make install
sudo cp /usr/local/lib/libgraph.* /usr/lib



Step #3
Now you're ready to compile your C program!

You have to type in the line #include<graphics.h> , just like you'd do in Windows.

In programs using graphics.h in Turbo C, you'll be using something like this:

int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");

In Ubuntu, you replace the "c:\\tc\\bgi" part with a NULL

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

Finally, while compiling the C file, don't forget to add a -lgraph parameter. The code will be something like this:

gcc heart.c -o heart -lgraph
./heart

And there you go! :)
Feel free to use the example code I made: heart.c

Was this post helpful? Let us know:


Bugs:
Although this implementation helps us to run all graphics.h functions, some predefined constants like SOLID_FILL may not be supported. Anyways, we can use integers instead of such names. This fixes the problem without much effort.

The graphics window can occasionally crash on focus. Rather than a bug in the graphics.h implementation, this is a common bug in gnu/linux graphics which apparently affects many other applications.

49 comments:

  1. :-) This will help a lot of folks; thought I wouldnt find use for me :-)

    ReplyDelete
  2. turboC is the worst! doing programming on ubuntu is so much fun! Hey do u know if i can install any graphics heavy games on ubuntu? like sims2?

    ReplyDelete
  3. @ Insignia
    Only when I was forced to use windows again, did i realize how much i loved Ubuntu :)

    @ NIM
    Well, we have the Wine package, which enables us to run windows programs in Ubuntu.
    The compatibility is highly game specific. I have played all versions of GTA smoothly in Ubuntu. But when I tried the Hitman series, it was all distorted. Wine supports many popular games like counterstrike. Check for yourself or simply google to see how much good it does with Sims2.

    ReplyDelete
  4. ahh... a pop up comment! ha ha ha!
    I have no idea what ubuntu is. Isn't it a place in Africa, next tu Timbuktu?

    And why the heck I haven't seen you near my blog these days?!

    ReplyDelete
  5. @ Shadow
    Ubuntu is indeed related to Africa, but nowadays its more popular as that cool linux distro. Try it out!
    And you see, i'm loaded up with works now, and is missing out on all my blogger buddies :(
    All I have to write in my blog too is some techy post! But I'll be back soon, just like everytime :)

    ReplyDelete
  6. thanx a lot!! it really helped!

    ReplyDelete
  7. very very great!!!!!!!!!!!!!
    Thank you!!!!!!!!!!!

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. thanx alot man, i was trying for it since last one month coz database connection with MySQL was not possible on windows...........n now i got it coz of u........

    ReplyDelete
  10. Thanks!!! Great Help =) =)

    ReplyDelete
  11. Dear et,

    Thank you so much for your essential help.
    I'd dare asking an also important question: in this implementation of , where are functions like setfillstyle, settextstyle, and so on?

    Thank you so much for your time.
    Antonio

    ReplyDelete
  12. thank you very much
    because it solved with in 10 minites

    ReplyDelete
  13. great work..it is very much useful

    ReplyDelete
  14. :-(

    error

    [xcb] Unknown sequence number while processing queue
    [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
    [xcb] Aborting, sorry about that.
    heart: ../../src/xcb_io.c:273: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
    Aborted



    any solutions

    ReplyDelete
  15. Error!!!!!!!!!!



    /tmp/ccSmZ1Dr.o: In function `main':
    r.cpp:(.text+0xa): undefined reference to `std::cout'
    r.cpp:(.text+0xf): undefined reference to `std::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)'
    /tmp/ccSmZ1Dr.o: In function `__static_initialization_and_destruction_0(int, int)':
    r.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
    r.cpp:(.text+0x42): undefined reference to `std::ios_base::Init::~Init()'
    collect2: ld returned 1 exit status



    :( o_0

    ReplyDelete
  16. error after ./configure !!

    checking SDL ==> SDL_image library... configure: error: *** SDL_image not found

    what do i do ??

    ReplyDelete
  17. Thanks a lot mate. I had earlier tried unsuccessfully from the blog you referred to in you post. But the above worked perfectly.

    Btw how to implement dos functions like getch(), clrscr() or at least delay() etc.. ? Thanks again

    ReplyDelete
  18. I have tried to follow the steps but I am getting following error. pls. help
    GConf Error: Configuration server couldn't be contacted: D-BUS error: Method "GetDefaultDatabase" with signature "" on interface "org.gnome.GConf.Server" doesn't exist

    ReplyDelete
  19. Pls help.After following all the steps program gets compiled without error. While running the same following error is reported
    ./p2:
    error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory

    ReplyDelete
  20. 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.:(

    ReplyDelete
  21. can you please help sudo command is not picking up in RHEL5

    ReplyDelete
  22. Worth Sharing work \m/ Dude :tup

    ReplyDelete
  23. Error :

    lakshmi@lakshmi-Inspiron-N5010:~/libgraph-1.0.2$ sudo make
    make: *** No targets specified and no makefile found. Stop

    What do i do ?

    ReplyDelete
  24. i have error ./configure: Permission denied on ./configure

    And i type chmod +x configure

    then every thing works fine

    thank uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

    ReplyDelete
  25. my graphic program is compiling sucessfully but the output of program blinks and ends??how to hold the ouput screen so that i can see the output..

    ReplyDelete
  26. im hetting the following error:-
    hecking for sdl-config... no
    checking for SDL - version >= 1.2.0... no
    *** The sdl-config script installed by SDL could not be found
    *** If SDL was installed in PREFIX, make sure PREFIX/bin is in
    *** your path, or set the SDL_CONFIG environment variable to the
    *** full path to sdl-config.
    configure: error: *** SDL version 1.2.0 not found!

    any suggestions please

    ReplyDelete
  27. Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Package libsdl1.2debian-all is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    libsdl1.2debian:i386 libsdl1.2debian

    E: Package 'libsdl1.2debian-all' has no installation candidate
    ashimnb87@linux:~/libgraph-1.0.2$


    ------Help me please-----------

    ReplyDelete
  28. besides,

    E: Package 'libsdl1.2debian-all' has no installation candidate
    ashimnb87@linux:~/libgraph-1.0.2$ ./configure
    checking build system type... x86_64-unknown-linux-gnu
    .
    .
    .
    .
    .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++... g++
    checking whether we are using the GNU C++ compiler... yes
    checking whether g++ accepts -g... yes
    checking dependency style of g++... gcc3
    checking how to run the C++ preprocessor... g++ -E
    checking for g77... no
    checking for f77... no
    checking for xlf... no
    checking for frt... no
    checking for pgf77... no
    checking for fort77... no
    checking for fl32... no
    checking for af77... no
    checking for f90... no
    checking for xlf90... no
    checking for pgf90... no
    checking for epcf90... no
    checking for f95... no
    checking for fort... no
    checking for xlf95... no
    checking for ifc... no
    checking for efc... no
    checking for pgf95... no
    checking for lf95... no
    checking for gfortran... no
    checking whether we are using the GNU Fortran 77 compiler... no
    checking whether accepts -g... no
    checking the maximum length of command line arguments... 32768
    checking command to parse /usr/bin/nm -B output from gcc object... ok
    checking for objdir... .libs
    checking for ar... ar
    checking for ranlib... ranlib
    checking for strip... strip
    checking if gcc static flag works... yes
    checking if gcc supports -fno-rtti -fno-exceptions... no
    checking for gcc option to produce PIC... -fPIC
    checking if gcc PIC flag -fPIC works... yes
    checking if gcc supports -c -o file.o... yes
    checking whether the gcc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
    checking whether -lc should be explicitly linked in... no
    checking dynamic linker characteristics... GNU/Linux ld.so
    checking how to hardcode library paths into programs... immediate
    checking whether stripping libraries is possible... yes
    checking if libtool supports shared libraries... yes
    checking whether to build shared libraries... yes
    checking whether to build static libraries... yes
    configure: creating libtool
    appending configuration tag "CXX" to libtool
    checking for ld used by g++... /usr/bin/ld -m elf_x86_64
    checking if the linker (/usr/bin/ld -m elf_x86_64) is GNU ld... yes
    checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
    checking for g++ option to produce PIC... -fPIC
    checking if g++ PIC flag -fPIC works... yes
    checking if g++ supports -c -o file.o... yes
    checking whether the g++ linker (/usr/bin/ld -m elf_x86_64) supports shared libraries... yes
    checking dynamic linker characteristics... GNU/Linux ld.so
    checking how to hardcode library paths into programs... immediate
    checking whether stripping libraries is possible... yes
    appending configuration tag "F77" to libtool
    checking for gawk... (cached) mawk
    checking for sdl-config... no
    checking for SDL - version >= 1.2.0... no
    *** The sdl-config script installed by SDL could not be found
    *** If SDL was installed in PREFIX, make sure PREFIX/bin is in
    *** your path, or set the SDL_CONFIG environment variable to the
    *** full path to sdl-config.
    configure: error: *** SDL version 1.2.0 not found!
    ashimnb87@linux:~/libgraph-1.0.2$


    ReplyDelete
  29. This blog was really helpful. I had a tough time with other C programming tools before resorting to ubuntu.
    Thanks a lot for your help

    ReplyDelete
  30. no comments...just hats up for the geek \m/

    ReplyDelete
  31. thank you !
    But can you say for me use text color ? I use function setcolor(1) but text not change color?

    ReplyDelete
    Replies
    1. chào bạn.trên diễn đàn thấy bạn là người Việt.bạn có tài liệu nào lập trình C trên Ubuntu 11 trở lên thì cho mình.mình đang cân nó cho việc học.bạn vui lòng gởi qua mail nbinhxd@yahoo.com cảm ơn bạn nhiều

      Delete
    2. Thực ra thì mình làm trên win, vừa mới chuyển sang Ubuntu thấy nó cũng giống với win thôi bạn. Có vài cái khác biệt thì mình GG. ^^

      Delete
  32. checking for sdl-config... no
    checking for SDL - version >= 1.2.0... no
    *** The sdl-config script installed by SDL could not be found
    *** If SDL was installed in PREFIX, make sure PREFIX/bin is in
    *** your path, or set the SDL_CONFIG environment variable to the
    *** full path to sdl-config.
    configure: error: *** SDL version 1.2.0 not found!
    prasant@prasant-Parallels-Virtual-Platform:~/libgraph-1.0.2$ sudo make
    make: *** No targets specified and no makefile found. Stop.
    prasant@prasant-Parallels-Virtual-Platform:~/libgraph-1.0.2$ sudo make install
    make: *** No rule to make target `install'. Stop.




    please help...the build fails!!!

    ReplyDelete
  33. avishek@avishek:~/libgraph-1.0.2$ ./heart
    [xcb] Unknown sequence number while processing queue
    [xcb] Most likely this is a multi-threaded client and XInitThreads has not been called
    [xcb] Aborting, sorry about that.
    heart: ../../src/xcb_io.c:274: poll_for_event: Assertion `!xcb_xlib_threads_sequence_lost' failed.
    Aborted (core dumped)

    ReplyDelete
  34. Thank you very much... THis blog was really really usefulll!!!

    ReplyDelete
  35. Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Package libsdl1.2debian-all is not available, but is referred to by another package.
    This may mean that the package is missing, has been obsoleted, or
    is only available from another source
    However the following packages replace it:
    libsdl1.2debian:i386 libsdl1.2debian
    help me out...

    ReplyDelete
  36. Thanks a lot.this was really very useful .Looking forward to more post like this

    ReplyDelete
  37. sudo make gives no target specified and no makefile found

    ReplyDelete
  38. fatal error: graphics.h: No such file or directory
    compilation terminated.

    ReplyDelete
  39. Thanks a ton ! It works! However , I had to install guile separately

    ReplyDelete
  40. Awesome bro thanx a million.

    ReplyDelete
  41. when I run the graphics program of putpixel()
    then program has compiled but after the run output is not shown because it is shown only for 1 sec.
    clear this problem.

    ReplyDelete