Jai Shri Ram!

Getting graphics.h to work on Windows and Linux

We have an introductory Computer Graphics course at our university this semester, we will be working with BGI(Borland Graphics Interface) which has its origins in Borland Turbo C. It goes without saying that I’m not a big fan of Turbo C, its a wonder why its so popular in India even though its so out of date. I have mingw set up on windows and also dual boot sabayon linux. I’ll set it up on both and list the steps here. This should work with Bloodshed Dev-Cpp but your mileage might vary.

Let us set it up for windows first, you need to download the two files graphics.h and libbgi.a. Copy graphics.h to your includes directory ( C:\MinGW\include in my case ) and the libbgi.a to the lib directory ( C:\MinGW\lib ). The only other thing that needs to be done is to compile your programs with the following linker options

-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

Here’s a sample program that you can use to check your setup.

#include <iostream>
#include <graphics.h>

using namespace std;

int main(int argc,char** argv)
{
	int gd=DETECT,gm;
	initgraph(&gd,&gm,0);
	rectangle(50,50,500,200);
	getch();
	closegraph();
	return 0;
}

This can be compiled in a this manner.

C:\Users\nikhil\Documents\graphics_h_setup>g++ rectangle.cpp -o
rectangle -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

The Output should be similar to the following.

After this you should be able to try your own programs at your leisure. Now we move onto the instructions for setting the same up on linux. Its much easier for us to install this in linux, the detailed steps are as follows. We’ll build from source we need to grab libgraph. Now its a simple configure make install cycle. You can use the following commands to accomplish the entire thing.

[nikhil@lovelock ~]$ mkdir Sources && cd Sources
[nikhil@lovelock ~]$ wget http://ftp.twaren.net/Unix/NonGNU/libgraph/libgraph-1.0.2.tar.gz
[nikhil@lovelock ~]$ tar xf libgraph-1.0.2.tar.gz
[nikhil@lovelock ~]$ cd libgraph-1.0.2
[nikhil@lovelock ~]$ ./configure --prefix=/usr/lib
[nikhil@lovelock ~]$ make -j2
[nikhil@lovelock ~]$ sudo make install
[nikhil@lovelock ~]$ make clean && cd ~

The same sample program can be run in the following manner

[nikhil@lovelock graphics-progs]$ g++ rectangle.cpp -o rectangle -lgraph
[nikhil@lovelock graphics-progs]$ ./rectangle

That’s all there is to it, you’re on your way. I’m not convinced that this is the best way either, one should probably learn something like OpenGL instead. Having said that this is better than Turbo C any day. Drop in a line if you have any questions or clarifications with this.

This project is maintained by nikhilbhardwaj