How to include "graphics.h" in Code:Blocks

Posted by MUKESH KUMAR on 15 Aug 2013

BGI (Borland Graphics Interface) is ancient as is turbo c++ IDE but nevertheless they are still used in our schools and colleges. Plus they do not work in windows environment (support for native DOS has been completely removed in windows vista/7).If you try to compile the source code with including “graphics.h” in code::blocks IDE then is show error because “graphics.h” only run in turbo.For code::blocks you have to setup winBGIm library. 
There are many alternative to winBGIm ,But BGI is good for beginners because you don’t have to worry about whole new fancy functions,classes etc. 
WinBGIm is BGI graphics library ported to computer by by Michael Main at the University of Colorado.
Before i start, i must tell you that this guide is written in accordance with code::blocks IDE.

Lets get started:

  1. First download WinBGIm from http://winbgim.codecutter.org/
  2. Extract it.
  3. Copy graphics.h and winbgim.h files in include folder of your compiler directory
  4. Copy libbgi.a to lib folder of your compiler directory
  5. In code::blocks open Settings >> Compiler and debugger >>linker settings
  6. click Add button  in link libraries part and browse and select libbgi.a file
  7. In right part (ie. other linker options) paste commands
  8. -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  9. click Ok
Finally, you can now compile source code using “graphics.h”.  Now, do your code as usual like you did in TC or you can try the Code below as sample. The WinBGIm library gives you an alternative as I hope, you found this guide helpful and in-case you need my help just leave a message.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<dos.h>
#include<string.h>
#include<iostream>
#include<ctime>
 
//using namespace std;
 
int main( void)
{
    int gdriver = DETECT, gmode, errorcode;
    initgraph(&gdriver,&gmode,"d:\\tc\\bgi");
 
    setfillstyle(SOLID_FILL,GREEN);
    setcolor(GREEN);
    rectangle(50,50,50+400,50+300);
    floodfill(51,51,GREEN);
 
    setfillstyle(SOLID_FILL,RED);
    setcolor(RED);
    circle(250,200,100);
    floodfill(250,200,RED);
    getch();
    closegraph();
 
return 0;
}

{ 17 comments... read them below or add one }

Anonymous said...

error message: fatal error: sstream: No such file or directory

Unknown said...

tried the above, got error at (line 30) #include

Unknown said...

mingw32-g++.exe -c C:\Users\JVH\Downloads\c++\yash.cpp -o C:\Users\JVH\Downloads\c++\yash.o
In file included from c:\program files (x86)\codeblocks\mingw\include\graphics.h:29:0,
from C:\Users\JVH\Downloads\c++\yash.cpp:1:
c:\program files (x86)\codeblocks\mingw\include\graphics.h:302:40: error: redefinition of 'int right'
int left=0, int right=0, int right=INT_MAX, int bottom=INT_MAX,
^
In file included from C:\Users\JVH\Downloads\c++\yash.cpp:1:0:
c:\program files (x86)\codeblocks\mingw\include\graphics.h:302:27: error: 'int right' previously declared here
int left=0, int right=0, int right=INT_MAX, int bottom=INT_MAX,
^
C:\Users\JVH\Downloads\c++\yash.cpp: In function 'int main()':
C:\Users\JVH\Downloads\c++\yash.cpp:33:44: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
initgraph(&gdriver,&gmode,"d:\\tc\\bgi");

Mirwise Khan said...

gives sstream error in graphics.h

Anonymous said...

c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\..\..\..\..\include\graphics.h|30|fatal error: sstream: No such file or directory|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|

Anonymous said...

after doing this I have an error in graphics .h file line no 224 void getarccoords( arccoordstype *arccoords );
so whai I do now.
??

Prince said...

Please send the download link of code blocks because the i have install code block 16.0 which have no such directories like include or lib any where.

Prince said...

Please send the download link of code blocks because the i have install code block 16.0 which have no such directories like include or lib any where.

Unknown said...

I followed the given steps,the code gets complied but fails to run .exe file. kindly guide me through this problem.

Unknown said...

but there is still a problem in line 302 of graphics.h header file please give a solution.

Anonymous said...

I don't have any TC named folder in my C or D drive. I am facing huge problem, plz help me

Unknown said...

didnot work . still shows error

Unknown said...

The "graphics.h" file has a little error....
Solution to it:
Go to>C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.7.1\include
inside this open "graphics.h" using notepad > go to line no. 302 > there change int right=INT_MAX to int top=INT_MAX
Save the file
That's it...hope it helps

Unknown said...

done very same thing
but as soon as i run it shows an error that compgraphics has stopped working

Saurabh Shinde said...

it is not working... when I run my code ...a pop up window appears saying that program has stopped working ....and execution terminate...please guide me regarding this

Anonymous said...

i am getting an error ld returned 1 exit status

Bali said...

So guys save your program with .cpp extension.

Post a Comment