Search code examples
c++g++bgiwinbgi

Why my C++ Graphics Program is not showing any output with g++ compiler?


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


Solution

  • 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.