If you want to learn about graphics programming through C or C++, then "graphics.h" library is good choice to start with. It is extremely beginners friendly and therefore any one can learn it very easily. But as it is an outdated library, many modern C/C++ compilers and IDEs don't have built-in support for "graphics.h" and CodeBlocks IDE is one of them. So, in this blog, you will learn How to setup graphics.h library in your CodeBlocks IDE.

Setup graphics.h in CodeBlocks

Step 1:
Download the graphics.h library file.

Download line here.

Step 2:
After downloading the zip file, extract this file to somewhere in a folder in your computer. You will get four files in it.

Step 3:
Now, hold the Ctrl button and click on "graphics.h" and "winbgim.h" to select these two files and copy it to this location

"C:\Program Files(x86)\CodeBlocks\MinGW\include"

In the folder where you extracted the zip file, there is one file with name "libbgi.a". You have to copy this file to this location

"C:\Program Files(x86)\CodeBlocks\MinGW\lib"

Step 4:
So, you have placed all the required files. Now you have to link graphics.h library with CodeBlocks and add appropriate linker options.

Open CodeBlocks..

In the menu bar, Click on Settings.
A popup menu will open. Click on Compiler option in this popup menu.

Popup Menu

A new window will open. Select Linker Settings Tab.

Compiler Settings Window

Now, here you have to link graphics library and add the appropriate linker options.


Step 5:
Click on Add button which is below the link libraries box. A new popup window will open.
Add Library
Click on browse button and go to the location

"C:\Program Files(x86)\CodeBlocks\MinGW\lib"

Now, search for the file with name "libbgi.a". Select it and click on Open button. Thereafter, click OK to Add library.

Step 6:
Now inside the "Other linker options" box, you have to write the appropriate flags. Write the following flags there.

-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

Other Linker Options

Now Click on OK button.

So, you have linked "graphics.h" in CodeBlocks. Now lets check whether it is working properly or not.

Test graphics.h in your program

Click on "Create a New Project" in "Start here" page. A new popup window will open.

Create a New Project

Here, select "Console Application" and click on Go Button.

Click on Next button.

Now, you will have to select your language. Select C++ here and click on Next.

Choose your Language

Note: It doesn't matter whether you program in C or C++. To be able to use "graphics.h" in your project, you will have to select C++ as your language because the graphics library the we are using, internally includes some header files which is part of C++ but not available in C language. So, if you select C here, you will get an error regarding "File missing".

Enter your project title and select a location where you want to create your project.


Project title and Project location

Click Next.

Click Finish.

Write this code in your "main.cpp" file
#include <graphics.h>
int main()
{
    int gd=DETECT, gm;
    initgraph(&gd, &gm, "");
    circle(200, 200, 100);
    getch();
    return 0;
}

Now build and run your program or simply press the F9 key.

Output of Program

You will get output as shown in above figure.

So, you have setup graphics.h successfully.