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.

2 thoughts on “Getting graphics.h to work on Windows and Linux”

  1. This is one of the neatest tricks I’ve ever found on the Web. I’m definitely not interested in BGI in the least (I’m entirely a FOSS person) and although I know the outdated condition of Indian computer science syllabi, I never expected anyone to go to such lengths to come up with such an ingenious “solution.”

    I also went through a few of your other posts. You are exceptionally knowledgeable about the realities of computer science and for a change it’s delightful to see such sound knowledge in a student. Keep up the good work!

Comments are closed.