Thursday, January 18, 2007

graphics.h in Linux

For my Univ project, I need 2 use graphics.h. Everyone knows graphics.h is not in C's standard library. Only Turbo C supports it and for that I need 2 use windows all the time which I don't like. So I had 2 find an alternate way of doing it and libgraph was just the thing I needed most. 'libgraph' is mainly a wrapper around SDL's libraries that effectively provides most of the functions of graphics.h.

Here is how I set up libgraph in my Debian Sid:

Downloaded the latest version of libgraph from this site:

http://download.savannah.nongnu.org/releases/libgraph/

First installed some libraries that libgraph depends on:

#apt-get install libsdl-image1.2 libsdl-image1.2-dev guile-1.8 guile-1.8-dev

(if you are using other distros like Fedora or Suse, packages 4 these distros are also avalable)

Extarcted the tarball

$tar xvzf libgraph-1.0.2.tar.gz

Then inside the folder ran this:

$./configure
$make
#make install

The default destination for the shared library files is /use/local/lib, if this path is not included in your library path (mine was not) then u have to add that manually in your /etc/ld.so.conf

Our installation is complete now. Now I make a simple test.c with these codes:

#include stdio.h
#include graphics.h

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;
}

[Note: There seems 2 be some problem when I try to put stdio.h inside angled brackets so I'm writing that normally]

Now when we need 2 compile this test.c file we need 2 provide '
-lgraph' flag

$gcc test.c -o test.out -
lgraph

Walla! I saw a black window with a rectangle in the middle.

22 comments:

Abhishek K said...

Hey still says graphics.h not found...
What am i supposed to do

Anonymous said...
This comment has been removed by a blog administrator.
Swakkhar Shatabda said...

can you give me a library of C which handles vertical retrace and has minimal functionalities like drawing circles, polygons which graphics.h provides as supplied by Borland C++.

Platform is DOS and Borland C++
Thanx in advance

Anonymous said...

HI...........
I did all those installations and the graphics code in getting compiled properly......but its not getting executed...
Its giving following error....,
"./tes.out: error while loading shared libraries: libgraph.so.1: cannot open shared object file: No such file or directory"

Anonymous said...

When compile libgraph, it writes you where it was installed in. In my case /usr/local/lib. And then I executed command: export LD_LIBRARY_PATH="/usr/local/lib" It seems it is function then to me.

twilight aura said...

he is debian sid a super set or sub set of fedora...m a ovice andlz help me to install libgraph

twilight aura said...

does dopin the aforementiond steps install libgarph in fedora...??

Unknown said...

excellent tip
works great

Balamurugan S said...

hey when I try the ./configure file i get the following error...


checking how to run the C++ preprocessor... /lib/cpp
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details


I don't know what it means, can u please help?

Shabab Haider Siddique said...

Nice post.. i am just going to try my dos game in linux.
well does anyone here know any substitute of conio.h?

i need getch
ifkbhit functions

Sreevisakh said...

There are lot more packages to install try the link

http://itsanimesh.com/2009/01/08/c_graphics_in_linux/comment-page-1/#comment-645

Brad Fallon said...

What are the differences between UNIX and Linux, and what about all those choices within Linux?

fico score said...

How can insert data into mysql file in a LINUX system from an EXCEL file in WINDOWS machine?

Chatranga Ranasingha said...

Nicely Put.. Thanks For The Tip... :D

srujana said...

still kbhit() is not recognized....
change initgraph to the following line:
initgraph(&gd,&gm,NULL);

and use getch() instead og kbhit()

express payday loans said...

What are the criteria to be considered for selecting the LINUX OS to install?

Anonymous said...

I got following error when i ran the program. and screen get closed.

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


Plz help me, how to get rid of this problem.
thanks in advance

eb 5 visa program said...

What are the differences between UNIX and Linux, and what about all those choices within Linux?

Guru said...

"The default destination for the shared library files is /use/local/lib, if this path is not included in your library path (mine was not) then u have to add that manually in your /etc/ld.so.conf"

how do i do that exactly??

eb 5 visa said...

Whats the difference between Linux and windows hosting? Do i need Linux on my system to linux host?

brickell condo said...

How to remove Linux Mint from my flash drive to install a different distro of Linux?

Unknown said...

how can we install graphic.h library in fedora 18

Back to Blogging

After a failed attempt last year to get back to blogging, I'm trying it again this year. I really wanted to get back, but got busy will...