'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 1:[1]
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++.
Solution 2:[2]
With respect to
fatal error: graphics.h: No such file or directory
#include<graphics.h>
graphics.h is not a standard library. It refers in this case to the WinBGIm library that implements the Borland MS-DOS BGI API using the Windows GDI API.
Moreover header files in general are not libraries, which is why you get "undefined reference" errors - you have to link the library itself, not just include the header.
For:
Gph.C:5:24: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
initgraph(&gd, &gm, "");
The warning is perhaps clear, but it is doubtful that you intended C++ compilation given that you tagged the question [C], but you have named your source with a .C instead of .c extension (lower-case). That causes C++ compilation as specified at https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html. The fact that the argument is not declared const in graphics.h is a problem of const-correctness where C is more permissive that C++. It is perhaps surprising the author has not fixed it. If you do use C++ compilation, then you would need to const_cast it.
So you need to:
- download the WInBGIm library,
- add the necessary include file path compiler switch (
-I) to our build command - add the necessary linker switches to your build command to link the library,
- change the filename extension to lower-case .c for C compilation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | itsDV7 |
| Solution 2 | Felix An |
