How to include graphics.h in CodeBlocks?

49
26297

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.

Graphics.h in codeblocks - Compiler Settings
Global Compiler Settings >> Linker Settings

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]

graphics.h file

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.

49 COMMENTS

  1. 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

  2. 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.

  3. 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.

  4. 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……

  5. 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.

  6. 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

  7. 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?

    • 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.

  8. 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.

    • This error has nothing to do with this tutorial, just check if you are using a character array in wrong way.

  9. 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 🙂

    • 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) ===|

  10. 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.

    • 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

  11. 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.

  12. 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.

  13. 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.

  14. 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 :/

LEAVE A REPLY

Please enter your comment!
Please enter your name here