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 –

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

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 –

Here is the official link for WinBGIm graphics library – 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.

This Post Has 42 Comments

  1. Abdulloh

    Thanks a lot! It worked

  2. Abdulloh

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

      Please try Code::Blocks to 16.01 for now.

    2. Sangam

      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 – 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. Parth Sehgal

        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.

        1. Radu Stirbu

          It doesn’t work….

  3. Murtuza Akber

    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 ?

    1. GG

      int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX, (try this at line 302)

    2. Sangam

      Which version of CodeBlocks?

      1. Bot

        same issues but ver 17.12. Can you help ?

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

  5. lile

    No it does not work with code blocks 17.12 and mingw64
    link error : ld.exe cannot find -lbgi

  6. Ali GreenHeart

    You are the best!

  7. yuu

    i cant save file graphics.h after edit int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,

    1. Sangam

      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.

  8. Jacques

    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!

  9. Sid Jones

    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. Sid Jones

        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!)
        Sid Jones

  10. shubham dubey

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

      1. aks

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

        1. Sangam Shrestha

          I am unware of this problem because I have not faced this problem. I have to check this.

  11. Harshit

    thank u soooo much !!!!!

  12. sangeer

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

  13. Terry

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

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

      1. Jacques

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

          I will check the problem..

        2. Sangam

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

            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.

        3. Sangam

          The latest version is 17.2 and it’s working properly here.

          1. Jacques

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

          2. Sangam

            Are you still getting the error?

        4. Easy as a b C

          The shiped version of libbgi.a is broken

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.