How to Include graphics.h in Code::Blocks – C++ Graphics Program

MUST READ – Sorry for not updating this post for the long time. Many users are complaining that is is not working with newer version of CodeBlocks. So there is another way to run Borland Graphics Interface. Since WinBGIm is very old maybe it is not working with new linker or whatever. So to make this work you need to install GCC compiler separately and place binaries there. Here is how to do that – https://sangams.com.np/adding-graphics-h-tdm-gcc-c-compiler/

Code::Blocks is mostly used opensource IDE due its great features. However it can’t run graphics programs unless you include graphics.h. But we can make it work by adding WinBGIm graphics library manually. That way we can use #include<graphics.h> in Code::Blocls.

Code::Blocks is lightweight and easy to use and becoming opensource it is available for Windows, Linux and Mac and is free to download and use. It is best IDE for beginners who wants to learn and C, C++ and FORTRAN or for creating small projects. With addition of WinBGIm in Code::Blocks we can use graphics.h header file then compile and run graphics program in C++ without any errors. Let’s see how to include graphics.h in Code::Blocks.

How to add graphics.h support in Code::Blocks?

  • Download WinBGIm from the download section below. Downloading from other website may not work since official graphics.h has minor error.
  • Extract it.
  • Open info.txt for linker options and more information.
  • Copying MinGW folder to your Code::Blocks installation directory. Default Code::Blocks installation directory is C:\Program Files (x86)\CodeBlocks. There will be MinGW folder already. Copying new MinGW folder only adds some library (libbgi.a) and header (winbgim.h, graphics.h) files in that directory. To manually add files, copy graphics.h and winbgim.h files in include folder of your compiler directory which is C:\Program Files (x86)\CodeBlocks\MInGW. And copy libbgi.a to lib folder of your compiler directory.
  • Open Code::Blocks. In open Settings >> Compiler >>Linker Settings. Click Add button in link libraries part and browse and select libbgi.a file you just copied to MinGW folder.
  • In right part (i.e. other linker options) paste commands -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32
  • Click OK.

If you didn’t see MinGW folder then it might be installed in different location. Check in C:\MinGW.

You can also watch my short video about adding graphics.h support in Code::Blocks

https://www.youtube.com/watch?v=LNtVlHcutAQ

Now you can run graphics program in Code::Blocks by including graphics.h header. I have added a sample program clock.cpp with that WinBGIm archive file, you can try compiling it.

Note: If you are getting sstream not found error, then you probably trying to run a .c file program. sstream is c++ header so graphics.h won’t works with c.

You may also like How To Install Dark Themes In CodeBlocks?

Download WinBGIm graphics.h library

You can download corrected graphics library files from here – https://drive.google.com/open?id=1joDbQBIsnjCElEpUx4z59oWbiXcwACha

Here is the official link for WinBGIm graphics library – http://www.codecutter.net/tools/winbgim/ but it has small problem with graphics.h file which is have corrected. Line 302 has been replaced by int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,.

To setup OpenGL and GLUT in Code::Blocks there is a good video available in YouTube – How to setup OpenGL and GLUT with CodeBlocks on Windows.

42 thoughts on “How to Include graphics.h in Code::Blocks – C++ Graphics Program”

  1. C:\Users\mypc\Desktop\Coding\WinBGIm_Library6_0_Dec2017\clock.cpp|34|warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]|

    1. Same here. I also get the error
      warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]|

      1. Hello Sangam. I did exactly what you said above and tried to run different simple functions on Code::Block (v17.12), as described in your other videos, but like others, upon compilation, I always get: “warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]”. When I run it on I get: “Process returned -1073741819 (0x C0000005)”. Do you know the solution to this problem?

        1. You guys are not doing the step carefully. It happens due to incorrect linker setting. Carefully do step 5 and after that. If you don’t understand step 5, I am sorry about that. You need to select libbgi.a library which is inside MinGW folder you just copied in previous steps..

          1. Jacques Cholette

            Thank you for taking the time to respond. I am at ease with computers. If I could send you captures from the snipping tool, you would see that winbgim.h and graphics.h files are indeed under the include folder, libbgi.a under the lib folder, the int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX on the linker options and lib\libbgi.a on the link libraries. I have also changed the graphics.h error from right to top. The error is always on the initgraph(&gd, &gm, “C:\\TC\\BGI”) line. Perhaps an older version of C:B would work! I’m glad to see it works for others

          2. What version you are using? I tested in CodeBlocks 17.2. And in linker option you don’t need to add left=0 etc. I will check with latest version and will update you.

          1. My line above for “linker option” should read “-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32”. I am using C::B 17.12.

  2. ||=== Build: Debug in gra (compiler: GNU GCC Compiler) ===|
    C:\Program Files (x86)\CodeBlocks\MinGW\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)) ===|

  3. i followed every step still i m unable to use graphics.h. it is saying “no such directory found”, wht should i do?

      1. save file with an extension “.cpp”, the things will get working. However, with some unknown reasons, the whole compilation goes well but does not run anything on my machine. 🙁

  4. Great work! Got the clock programme going nicely on my PC.
    Any suggestions on how I can get a larger graphics area on my screen, please?
    I’m not worried about resizable windows, just bigger area to display more detailed graphs.
    Doing simulations of embedded control systems using CodeBlocks to model real-live applications.
    (Using 1920 x 1600 monitors with NVIDEA graphics cards.)

      1. OK, that works when I add initwindow(1000,1000); after the call to initgraph(…) in the clock demo. I do now get three ‘dos’ windows which are related, so I need to have a read up on how this works. it’s not a problem, just being tidy! Any pointers on where to dive deeper into understanding this topic, please?
        Thank you for the prompt reply. Good fortune with your CS studies.
        (I really must try to do more console style applications rather than just embedded micro code!)
        Regards
        Sid Jones

  5. Getting the same “warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]”. When I press the “build” icon I still get: “Process returned -1073741819 (0x C0000005)” on the initgraph(&gd, &gm, “C:\\TC\\BGI”) line. BUT…when I press the “build and run” icon I can draw!!! The various functions work! Did you hack into my system Sangam?! Joking. Many thanks for keeping up with me!

    1. You don’t need to modify graphics.h if you download from here.
      You are not able to save it because it may be in C:/ drive so you need admin privilages. Copy graphics.h to desktop and edit/save then paste to CodeBlock directory.

  6. Mehrdad Sheikhan

    Dear Sangam,
    Thank you very much for your solution. I got the error “cannot find -lbgi” in version 20.03 of Code::Blocks. I watched your video and the error was removed when I downgraded my Code::Blocks to 16.01!
    Best regards,
    Mehrdad Sheikhan

  7. Murtuza Akber

    Hey,
    I followed the exact procedure as mentioned. When I run the program I get an error saying “fatal error graphics.h: no such file or directory”. Can you please help me out here ?
    Thanks.

  8. Hello!
    I am using Code Blocks 20.03. I have read a lot about including graphics.h library to Code Blocks. The instructions in all websites I have read so far are almost the same. And I have totally followed all the instructions. But Code Blocks is reporting “cannot find -lbgi”. How I can solve this problem. Thanks beforehand

    1. I don’t know why this is not working with news CodeBlocks. Maybe it is due to new linker which comes along with codeblocks-20.03mingw-setup.exe. I tried with TDM-GCC compiler and it is working fine there. I had an article about using graphics.h with TDM-GCC since long time here – https://sangams.com.np/adding-graphics-h-tdm-gcc-c-compiler/. You just need to install regular version of CodeBlocks with not MinGW ‘codeblocks-20.03-setup.exe’ and install TDM-GCC compiler separately and copy the library and header files.

      1. To solve the error of cannot find – lbgi in MinGW ‘codeblocks-20.03-setup.exe’, you need to go to Codeblocks>Settings>Compiler>Search directories> linker>Add – now copy paste the path of your lib folder. For example my lib path is ” C:\Program Files\CodeBlocks\MinGW\lib”. Copy this path after clicking on ‘Add’ and this error should go away.

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top