How to configure graphics library graphics.h in CodeBlocks (the most complete and detailed)

Compiling the graphics code on CodeBlocks IDE will display an error: "Cannot find graphics.h" . This is because there is no graphics.h running in the library folder of CodeBlocks. To successfully compile the graphics code on CodeBlocks, please set up the winBGIm library.

So, how to include graphics.h in CodeBlocks?

Please follow the steps below in order to include "graphics.h" in CodeBlocks to successfully compile graphics code on Codeblock.

step 1:
To set "graphics.h" in CodeBlocks, first set up winBGIm graphics library. Download WinBGIm from https://download.csdn.net/download/UCB001/12152858
Step 2:
Unzip the downloaded file. There will be three files:

  • graphics.h
  • winbgim.h
  • libbgi.a


Step 3:
Copy and paste the graphics.h and winbgim.h files into the include folder of the compiler directory. (If you have installed Code :: Blocks in the C drive of your computer, please do the following: Disk C >> Program Files >> CodeBlocks >> MinGW >> include. Paste these two files here.) For example: C:\Program Files (x86)\CodeBlocks\MinGW\include


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

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


Step 6:
In this window, click the "Add" button under the "Link Library" section and browse.

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


Step 7:
Paste the command in the right part (ie other linker options)

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

Step 8:
Click [OK]

Generally, it is successful at this step, and the following steps may not be needed

Step 9:

Try to compile the graphics.h program using C or C++,possibleThere will still be errors. To solve this problem, use Notepad++ (or an editor such as Sublime, VSCode) to open the graphics.h file (paste it into the include folder in step 3). Go to line 302 and replace it with the following line: int left = 0, int top = 0, int right = INT_MAX, int bottom = INT_MAX,

Step 10:
save document. finished!

Note : Now, you can compile any C or C++ program that includes graphics.h header file. If you compile the C code, you will still receive an error message: " fatal error: sstream: no such file directory ".

For this problem, if the file extension is .c, change it to .cpp.

Attach graphics.h sample teaching code:

#include <stdio.h>
#include <graphics.h>
int main()
{
    
    
    //设置窗口大小
    initwindow(200,200,"XTU",0,0);
    int maxX = getmaxx();
    int maxY = getmaxy();
    //设置窗口背景色
    setfillstyle(1,LIGHTGRAY);
    bar(0,0,maxX,maxY);
    //画矩形
    setcolor(RED);
    rectangle(0,0,maxX,maxY);
    rectangle(50,50,70,150);
    //画填充矩形
    setfillstyle(1,GREEN);
    bar(100,50,120,150);
    //画直线
    setcolor(RED);
    line(150,50,150,150);
    //画文字
    setcolor(BLUE);
    setbkcolor(LIGHTGRAY);
    settextstyle(SMALL_FONT, HORIZ_DIR, 5);
    outtextxy(150, 150, "3278");
    //结束绘图
    getch();
    closegraph();
}

running result

Insert picture description here

Guess you like

Origin blog.csdn.net/UCB001/article/details/104286336