You are on page 1of 3

How to include graphics.h in CodeBlocks?

You might have come across source codes in C or C ++ languages having #include<graphics.h>
in the program. When you compile such codes in C odeBlocks IDE, it shows error saying C annot
find graphics.h. Why? This is because graphics.h runs only in Turbo C as it is inbuilt there, and
is not available in the library folder of C odeBlocks.

So, how to include graphics.h in CodeBlocks? If youve previously tried downloading


graphics.h header file and pasting it in the include folder of your compiler directory, but yet still
it showed error, heres a step-by-step solution to your problem. There are a few other settings
C odeBlocks wants you to make to successfully run or execute C /C ++ graphics programs.

To setup graphics.h in C odeBlocks, youll first have to set up winBGIm library (download
graphics.h). WinBGIm is a graphics library ported to computer by Michael Main at the University
of C olorado. It emulates graphics for the MingW (GC C port) compiler. Like the Turbo C ++ IDE,
the BGI, Borland Graphics Interface, is old, but its not yet outdated.

It is still used in many C /C ++ programs in schools and colleges. You can find many alternatives
to winBGI library, but this one is good for beginners in programming field. WinBGIm is the
easiest 2D graphics library you can find!

Steps to include graphics.h in CodeBlocks:


Step 1: Download WinBGIm from http://winbgim.codecutter.org/ or use this link.

Step 2: Extract the downloaded file. Youll get three files:

graphics.h
winbgim.h
libbgi.a
Step 3: C opy and paste graphics.h and winbgim.h files into the include folder of your
compiler directory. (If you have C ode::Blocks installed in C drive of your computer, go through:
Disk C >> Program Files >> C odeBlocks >> MinGW >> include. Paste these two files there.)

Step 4: C opy and paste libbgi.a to the lib folder of your compiler directory.

Step 5: Open C ode::Blocks. Go to Settings >> C ompiler >> Linker settings.

Step 6: In that window, click the Add button under the Link libraries part, and browse and
select the libbgi.a file copied to the lib folder in step 4.

1 of 3
Step 7: Go to Other linker options on the right part and paste these commands:
[highlight color=yellow]-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32[/highlight]

Step 8: Make sure you got steps 6 and 7 right! Heres a screenshot of previous two steps. Then,
click Ok.

Step 9: If you now try compiling a graphics.h program code in C or C ++, youll still get error. To
solve it, open graphics.h file (pasted in include folder in step 3) with Notepad++. Go to line
number 302, and replace that line with this line:
[highlight color=yellow]int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,
[/highlight]

2 of 3
Save the file. Done!

Now you can compile any C or C ++ program containing graphics.h header file. If you compile C
codes, youll still get an error saying: fatal error: sstream : no such file directory. For this
issue, if your file extension is .c, change it to .cpp.

Editors Note:
This post has now been thoroughly revised as people reported too many problems with the
previous steps. The previous steps worked out for some, but didnt for many. So, Ive corrected
all the errors. If you carefully follow the aforementioned steps, all graphics programs in C /C ++
will be executed, provided theres no error in the source code.

If you have any questions, bring them up from the comments section. Also, now that the post is
revised and updated, leave a feedback in the comments if the aforementioned procedure to
include graphics.h in C odeBlocks worked out for you.

3 of 3

You might also like