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 CodeBlocks IDE, it shows error saying “Cannot 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 CodeBlocks.
So, how to include graphics.h in CodeBlocks? If you’ve previously tried downloading graphics.h header file and pasting it in the include folder of your compiler directory, but yet still it showed error, here’s a step-by-step solution to your problem. There are a few other settings CodeBlocks wants you to make to successfully run or execute C/C++ graphics programs.
To setup graphics.h in CodeBlocks, you’ll 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 Colorado. It emulates graphics for the MingW (GCC port) compiler. Like the Turbo C++ IDE, the BGI, Borland Graphics Interface, is old, but it’s 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. You’ll get three files:
- graphics.h
- winbgim.h
- libbgi.a
Step 3: Copy and paste graphics.h and winbgim.h files into the include folder of your compiler directory. (If you have Code::Blocks installed in C drive of your computer, go through: Disk C >> Program Files >> CodeBlocks >> MinGW >> include. Paste these two files there.)
Step 4: Copy and paste libbgi.a to the lib folder of your compiler directory.
Step 5: Open Code::Blocks. Go to Settings >> Compiler >> 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.
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! Here’s 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++, you’ll 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]
Save the file. Done!
Now you can compile any C or C++ program containing graphics.h header file. If you compile C codes, you’ll 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.
Editor’s 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 didn’t for many. So, I’ve corrected all the errors. If you carefully follow the aforementioned steps, all graphics programs in C/C++ will be executed, provided there’s 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 CodeBlocks worked out for you.
thankyou it works //so far!
Thanks ……
How can I change my file extension .c to .cpp ???
Thanks for the detailed tutorial . After doing all the necessary steps my program builds without error but when I run it my console window crashes with error .exe has stopped working. Also message displayed is process returned (0xFF).
Kindly help me with what could be the reason of this.
Please reply fast i have more need of graphics program to run
Thanks for the detailed tutorial . After doing all the necessary steps my program builds without error but when I run it my console window crashes with error .exe has stopped working. Also message displayed is process returned (0xFF).
Kindly help me with what could be the reason of this.
sstream.h how to include
error in saving the file…saying “cant save in this location”…”access denied”…
I dnt find the method working …
if u find any solution to this plez help out
I have done everything stepwise that is written in the article. After compiling I don’t get any error but when I run my code, an error screen comes up stating – “Drawing operation was attempted when there was no current window.”
I wrote the following code :-
#include
#include
#include
using namespace std;
int main()
{
int i,j;
circle(2,4,4);
return 0;
}
I am using code blocks version 13.12
Please let me know what is wrong and whether I have written the commands correctly.
Reply soon.
YOU HAVE TO WRITE
#include
#include
#include
I am facing the error
||=== Build: Debug in CG (compiler: GNU GCC Compiler) ===|
c:\program files (x86)\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), 0 second(s)) ===|
I have changed the extension to .cpp instead of .c still it is showing the same error.
Kindly reply me what should I do now……
Thanks for the detailed tutorial . After doing all the necessary steps my program builds without error but when I run it my console window crashes with error .exe has stopped working. Also message displayed is process returned (0xFF).
Kindly help me with what could be the reason of this.
i did the necessary changes as u mentioned and after changing line 302 now i’m getting 11 erorrs on line 302:
1. stray ‘\342’ in program
2. stray ‘\200’ in program
3. stray ‘\235’ in program
4. stray ‘\342’ in program
5. stray ‘\200’ in program
6. stray ‘\235’ in program
7. expected identifier before ‘[‘ token
8. ‘highlight’ was not declared in this scope
9. expected ‘]’ before ‘color’
10. expected ‘)’ before ‘color’
11. expected initializer before ‘color’
plz help .i’ve codeblocks mingw 13.12
my program is in .cpp
Hello dear, Now i am open graphics.h file in Notepad++ and replace line no 302 with this line “int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,” and then click Save button, But this file is not save when i click save button then message ‘Please check if this file is opened in another program.’
Please tell me what i do?
Just don’t open the file in another program! Close all other applications if needed.
Plz help me .I can’t find the “include” folder in my compiler directory. I have Microsoft Visual Studio 2010 as my default compiler
It’s actually the compiler directory that comes with CodeBlocks.
try to open thet header file in notepad++(admin)
Open a copy of graphics.h file in texteditor like sublime
Make changes in line 302 .save the file in some other directory and replace this file from the older one in include folder.
You should be able to clear those errors.
solve error:
Drawing operation was attempted when there was now current window.
Thank You
What exactly did you try here? Could you send me the source code?
Hi,
I installed codeblocks in Fedora 18 using yum. I am getting error on graphics.h. Please do assist me.
Hi, I new to this code block. I tried the steps and now i facing the error as “cannot find -lbgi”. Please help me to fix this.
I did whatever you mentioned…
But I get a lot of errors in lot of lines…
How do i fix it???
That shouldn’t have been the case. What sort of errors did you get?
how do we include glut directory?please post in a similar manner..!!
Hey, go through this post.
in function int main
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]|
plzzz help!!
This error has nothing to do with this tutorial, just check if you are using a character array in wrong way.
thank you 🙂
I was testing a program from http://www.cquestions.com/2009/01/create-simple-paint-brush-software-in-c.html?showComment=1416891149824#c2372828338187090881 i put in the graphics.h when i tried to build no errors were found but it keeps asking if i want to build it. it isnt running. wat doi do?
Sir, it shows error at line no. 30
I tried to compile and i got an error saying “ld.exe cannot find -loleout32”, what do i do?
Thank you so much , I really needed that , in college they teach us TurboC it’s boooring -_- , I use Code::Blocks on Windows 8 and this method worked for me ,, thank you again 🙂
I’m getting error in line 30 #include
please help me..
line 30
#include // Provides std::ostringstream
|30|fatal error: sstream: No such file or directory|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
Guys, if you get an Error in line 302, just do what the guy said. Delete the first declaration. In simpler words, delete the int right = 0.
So the line 302 looks like this:
int left=0, int right=INT_MAX, int bottom=INT_MAX,
If you get an error in line 30, saying that “sstream.h” does not exist in the directory, that means you’re using C. sstream.h is for C++. So you’d have to use C++ for it to work.
I tried the same thing ,but apparently you can’t edit the graphics.h file :/
Shouldn’t there be int top=0 instead of int right=0 where redeclaration occurs? Parameters of all image functions look simillar, so “int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX” should probably be the same in printimage.
how can we make it work for c program
“an error in line 30, saying that “sstream.h” does not exist in the directory”
which is the header file for c program
i am also facing same error did u get the solution?
hi pramesh, I still got error using your method. i used win8.1 and latest verison of code block .
pleaze rply me fast i have to make project in c.
Sorry, this method works for Win7, but I haven’t tested this procedure on Win8.1.
Hi guys! If u get a error on line 302 with some redeclaration stuff just delete the first declaration and save! Tip: It may not let you save in code::blocks but you can use notepad.
How can i delete the first declarations?!
there is error on line 30 of graphics.h saying that fatal error: sstream: No such file or directory…
when i compiling it with #include
It showed an error on line 302 of graphics.h saying that int left and int right(assigned values 0) have already been declared. – See more at: https://www.codewithc.com/how-to-include-graphics-h-in-codeblocks/#sthash.uwvikIbG.dpuf
Hey! It tried compiling it with #include
It showed an error on line 302 of graphics.h saying that int left and int right(assigned values 0) have already been declared.
I tried removing the declaration part, leaving only the assignment part, and surprise, it said they haven’t been declared yet.
there is error in graphics.h header file, having redeclaration of variable right in putimage() function …so, i’m not able to compile any file having graphics.h header file :/