cbgi

Unable to run graphics.h program in windows shell


I'm trying to run this program on through gcc on Windows shell. GCC version that I'm using:

gcc (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 8.1.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The code:

#include<stdio.h>
#include<graphics.h>
int main(){
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "");
    line(150,150,450,150);
    getch();
    closegraph();
}

At first, it was giving me this error:

fatal error: graphics.h: No such file or directory
     #include<graphics.h>

Then I added the graphics.h file to my gcc directory. But now it is showing this error:

Gph.C: In function 'int main()':
Gph.C:5:24: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
  initgraph(&gd, &gm, "");
                        ^
C:\Users\simple\AppData\Local\Temp\cchdXkih.o:Gph.C:(.text+0x2e): undefined reference to `initgraph'
C:\Users\simple\AppData\Local\Temp\cchdXkih.o:Gph.C:(.text+0x52): undefined reference to `line'
C:\Users\simple\AppData\Local\Temp\cchdXkih.o:Gph.C:(.text+0x63): undefined reference to `closegraph'
collect2.exe: error: ld returned 1 exit status

Can someone suggest an alternative way to run my program? I've already tried running it on Visual Studio and Online Compilers.


Solution

  • You can use CodeBlocks IDE for it. Link.

    Or you can also use TurboC++ but you will have to make a minor change to your code:

    #include<stdio.h>
    #include<graphics.h>
    int main(){
        int gd = DETECT, gm;
        initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
        line(150,150,450,150);
        getch();
        closegraph();
    }
    

    An advantage of using TurboC++ is that you will not have to download any extra files to run your graphics.h programs unlike other IDE like CodeBlocks or DevC++.