Graphics.h in Linux for C and C++ January 8th, 2009
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.
Tags: Linux, Ubuntu, Windows
Posted in Techi Gyaan | Comments (12)













January 8th, 2009 at 8:51 pm
Way to go !!
Though I am finding it difficult to read. This is for young eyes!!
January 8th, 2009 at 10:59 pm
OMFG. You don’t even give credit to the person you stole all this from. And you call this blogging. Funny.
January 8th, 2009 at 11:01 pm
Well sorry about dis one.. i 4got to add it.
Its not from a particular source but a group of sources combined..
January 10th, 2009 at 2:44 am
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.
January 16th, 2009 at 8:48 pm
good work man……uve messed up hard with linux….gr8 hacks….
January 16th, 2009 at 8:51 pm
Thanxs man…
September 11th, 2009 at 10:12 pm
Can you help me in displaying a bmp image in linux platform using c?
October 7th, 2009 at 11:18 pm
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?
November 19th, 2009 at 12:50 am
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!
April 4th, 2010 at 1:52 pm
kbhit is not working it’s giving error: kbhit was not declared in this scope
August 14th, 2010 at 6:37 pm
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
August 14th, 2010 at 6:43 pm
thanks a lot for posting the command for copying the libraries………….i didnt see it before
now figured out