0

I have written a code in C++, which is given below. The program is compiling successfully. But, when I'm trying to run the .exe file, then I am not getting the desired output.

My Code:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>

int main( )
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, (char*)"");
    setcolor(BLUE);
    line(100, 100, 200, 200);
    getch();
    closegraph();
}

I am using the following command in Windows Command Prompt to compile the program:

g++ -m32 -o trial trial.cpp -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32 "C:\TDM-GCC-64\x86_64-w64-mingw32\lib\libbgi.a"

To run my .exe file: trial for cmd and ./trial in powershell.

But after this I am not getting any output. Why is it so?

enter image description here

Botje
  • 26,269
  • 3
  • 31
  • 41
Kalpadiptya Roy
  • 95
  • 1
  • 11
  • 1
    `start /wait trial.exe` `echo %errorlevel%` what output does running this gives? Is it non-zero? Try adding `-lgraph` linker directive. Also no need of writing `(char*)""`, simply replacing it will `NULL` should do the same. – brc-dd Sep 07 '20 at 13:27
  • 5
    I'd argue that the root problem is the use of antiquated and obsolete libraries for the graphics. While modern libraries are often more complex to set up (but not by much), they are also more powerful and flexible, and (most importantly) more *modern* and up to date with modern environments. – Some programmer dude Sep 07 '20 at 13:29
  • @brc-dd I have tried adding ```-lgraph``` to the linker directive but, this error is occuring ```C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/5.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgraph collect2.exe: error: ld returned 1 exit status``` – Kalpadiptya Roy Sep 07 '20 at 14:49
  • @brc-dd I have also tried the command ```start /wait trial.exe```, ```echo %errorlevel%```, but there is no output. Only a blank terminal appears and then disappears simultaneously for the first command. For the second command it returns -1073741819, which is non-zero. – Kalpadiptya Roy Sep 07 '20 at 14:59
  • Did you consider using a cross-platform graphical library such as [Qt](https://qt.io/), [FLTK](https://fltk.org/), [FOX](https://fox-toolkit.org/), or [libSFML](https://www.sfml-dev.org/) ? Don't forget to enable all warnings and debug info, so compile with `g++ -Wall -Wextra -g` – Basile Starynkevitch Sep 07 '20 at 18:10

1 Answers1

1

Edit : You are missing -L before path pointing to your libbgi.a library. More details are available here : Directory Options


Literally no one uses graphics.h these days. There are much better alternatives, as pointed by others.

But there are a lot of questions on StackOverflow referring to error code -1073741819 (0xC0000005) on using WinBGIm, not having any answer. Hence I'd like to post one here.

This error code is generated on segmentation fault. There may be many factors causing it, but with WinBGIm, it's generally due to a bug in the library itself.

To make it work you have to change line 302 of the graphics.h and winbgim.h (downloaded from the official source) from

    int left=0, int right=0, int right=INT_MAX, int bottom=INT_MAX,

to

    int left=0, int top=0, int right=INT_MAX, int bottom=INT_MAX,

The difference should appear trivial; earlier there was a typo as there were two arguments named right.


Bug-free libraries and headers are available here.

Actually I found a similar answer now. Future readers may refer to this.

brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • I have corrected this typo already and after correcting this only I have posted this question here. So I don't think this error is due to the typo. – Kalpadiptya Roy Sep 07 '20 at 17:23
  • Moreover this code runs fine in **Codeblocks 17.12**, as one of my friend have run this code in the **IDE** successfully. But my aim is to run is without **IDE**. I can provide the commands that the **IDE** generates during compile and run as provided by my friend. – Kalpadiptya Roy Sep 07 '20 at 17:28
  • Yes exactly ```-L``` was the actual problem. I have added it before the path of ```libbgi.a``` and now my code is running and I am getting the output. Thank you so very much. And please kindly edit your suggested answer and modify it accordingly. After that I will tick mark it as correct. – Kalpadiptya Roy Sep 07 '20 at 17:59
  • Yeah! Definitely, I would switch. But this old library is required for my college practicals on Computer Graphics. That's why seeking the solution was important. – Kalpadiptya Roy Sep 07 '20 at 18:14