CodeBlocks IDE throws the error “Cannot find graphics.h” while attempting to compile graphics code. This is due to the fact that CodeBlocks does not have graphics.h in its library folder. Compiling graphics code on CodeBlocks requires the installation of the winBGIm library.

How to include graphics.h in CodeBlocks ?

To properly build graphics code on Codeblocks, please follow the steps in the order provided below to include “graphics.h” in CodeBlocks.

Step 1: First, the winBGIm graphics library must be installed for “graphics.h” to be used in CodeBlocks. Download the zip folder, the link is given below.

Step 2: Extract the downloaded file. There will be three files: 

  • graphics.h
  • winbgim.h
  • libbgi.a
graphics.h necessary files
Graphics.h Zip Folder

Step 3: Copy and paste graphics.h and winbgim.h files into the include folder of 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.) 

Graphics.h file into the include folder of Codeblocks
Graphics.h file into the include folder of Codeblocks
winbgim.h file into the include folder of CodeBlocks
winbgim.h file into the include folder of CodeBlocks

Step 4: Copy and paste libbgi.a to the lib folder of compiler directory. 

libbgi.a to the lib folder of CodeBlocks
libbgi.a to the lib folder of CodeBlocks

Step 5: Open Code::Blocks. Go to Settings >> Compiler >> Linker settings. 

Opening Compiler Setting in CodeBlocks
Opening Compiler Setting in CodeBlocks
Opening Linker Settings from Compiler Settings
Opening Linker Settings from Compiler Settings

Step 6: In that window, click the Add button under the “Link libraries” part, and browse. 

Select libbgi a in Link Libraries
Select libbgi.a in Link Libraries

Select the libbgi.a file copied to the lib folder in step 4. 

Libbgi in Link Libraries
Libbgi.a in Link Libraries

Step 7: In the right part (ie. other linker options) paste the commands given below

-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 
Paste the Commands in Other Linker Options
Paste the Commands in Other Linker Options

Step 8: Click Ok.

Click Ok
Click Ok

Step 9: Try compiling a graphics.h program in C or C++. You can use the given code below:

#include<graphics.h>
#include<conio.h>

int main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "");
    for(int i=200;i<400;i=i+25){
        for(int j=200;j<400;j=j+25){
            circle(i, j, 100);
        }
    }

    getch();
    closegraph();
    return 0;
}
Output of Graphics Simple Code
Output of Graphics Simple Code

If your problem is not solved, then kindly follow the below steps. It will surely solve your problem.

How to include graphics.h in CodeBlocks using TDM GCC?

If you are still facing the error thats mean you are facing compatibility issue. Your computer is running on 64 bit but CodeBlocks compiler is running on 32 bit. For this you have to download a software called “TDM GCC”. You can download it from here.

Make sure to download the shown version.

Download TDM GCC
Download TDM GCC

After Installing the software simply follow the shown order.

Step 1: Copy and paste graphics.h and winbgim.h files into the include folder of TDM GCC. (If you have TDM GCC installed in C drive of your computer, go through: Disk C >>TDM-GCC-32 >> include. Paste these two files there.) 

graphics header file in TDM GCC Include Folder
graphics header file in TDM GCC Include Folder
winbgim header file in TDM GCC Include Folder
winbgim header file in TDM GCC Include Folder

Step 2: Copy and paste libbgi.a to the lib folder of compiler directory. (C >>TDM-GCC-32 >> lib)

Libbgi a file in TDM GCC Library Folder
Libbgi.a file in TDM GCC Library Folder

Step 3: Now you have to again open the Compiler Settings and go to the Linker Settings. And edit the Link Libraries to change the libbgi.a file location.

Edit the Link Libraries
Edit the Link Libraries

Select the libbgi.a file copied to the lib folder in step 2.

Changed Link Libraries
Changed Link Libraries

Step 4: Now we have to change the Compilers directory and set it to TDM GCC.

Change the Compiler's Directory
Change the Compiler’s Directory

Select the TDM-GCC-32 folder only. ((C >>TDM-GCC-32)

Changed Compiler Directory
Changed Compiler Directory

Step 5: Now you can run the code again and it may show output like below.

Output of Graphics Simple Code
Output of Graphics Simple Code

We really hope that this post helps you address the issue you were having with the graphics header file in the C programming language as well as the C++ programming language.