Graphics.h Not Working in VS Code version 1.15.1

10.8k views Asked by At

Context Language : C++ , Editor : Microsoft Visual Studio Code Version 1.15.1

Problem I am using Microsoft Visual Studio Code Version 1.15.1 for Compiling my C++ Programs. I want to use Graphics.h to use Graphics in my C++ Programs. That's why I downloaded the Three files Graphics.h ,winbgim.h and libbgi.a and Put them into the Right Places.I also Downloaded an BGI folder from https://www.cs.colorado.edu/~main/bgi/visual/BGI2010.zip and Put it into the Right Place. But When I am Compiling My Program then it Showing Some error.

My C++ Program

    #include<iostream>
    #include<math.h>
    #include<graphics.h>
    using namespace std;
    int main(){
        int x,y;
        int gd = DETECT ,gm;
        initgraph(&gd,&gm,"C:\\MinGW\bgi");
        cout<<"Enter the Value of X Co-ordinate : ";
        cin>>x;
        cout<<"Enter the Value of Y Co-ordinate : ";
        cin>>y;
        putpixel(x,y,WHITE);
        closegraph();
    }

Error of my Program

computerGraphics1.cpp: In function 'int main()':
computerGraphics1.cpp:8:25: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
     initgraph(&gd,&gm,"C:\\MinGW\bgi");
                         ^
C:\Users\TUSK\AppData\Local\Temp\ccEbDqZq.o: In function `main':
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:8: undefined reference to `initgraph'
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:13: undefined reference to `putpixel'
C:\Users\TUSK\Desktop\Practise\C++ Projects/computerGraphics1.cpp:14: undefined reference to `closegraph'
collect2.exe: error: ld returned 1 exit status

So Guys who ever Finds its Solution Please Answer it

3

There are 3 answers

1
Paul Floyd On

You can "fix" the error with a const_cast, but beware, if ever you do try to modify the string contents you can expect a crash.

0
ullas kunder On

Detailed setup if you want to use graphics.h in VC Code.

This worked for me...

char driverPath[] = "C:\\MinGW\\lib\\libbgi.a"; //static file
initgraph(&gd, &gm, driverpath);

1
sushant kumar On

Cin is not working when we use Graphics in VS Code, so we need to define the values of 'x' and 'y' manually.

    #include<iostream>
    #include<graphics.h>
    using namespace std;
    int main(){
        int x,y;
        int gd = DETECT ,gm;
        initgraph(&gd,&gm,NULL);
        // cout<<"Enter the Value of X Co-ordinate : ";
        // cin>>x;
        // cout<<"Enter the Value of Y Co-ordinate : ";
        // cin>>y;
        x=156;
        y=172;
        putpixel(x,y,WHITE);
        getch();
        closegraph();
    }

Output

Terminal Result:

  Starting build...
  C:\MinGW\bin\g++.exe -fdiagnostics-color=always -g "C:\Programming\Computer 
  Graphics\stackover.cpp" -o "C:\Programming\Computer Graphics\stackover.exe" - 
  lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

  Build finished successfully.

  Terminal will be reused by tasks, press any key to close it.

Follow these steps to run graphics.h properly in VS Code.