how to Fix these Errors in Graphics?

When I compile my code it Generates following errors:
1. undefined reference to 'initgraph'
2. undefined reference to 'line'
3. undefined reference to 'line'
4. undefined reference to 'line'
5. undefined reference to 'closegraph'
6. [Error] Id returned 1 exit status.

because of above Errors/Warnings I am unable to Run My programme, I don't know what is problem with my code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// C++ Programme for drawing line
#include<iostream>
#include <graphics.h>
#include<windows.h>
using namespace std;
// driver code
int main()
{
    int gd = DETECT, gm;
    initgraph(&gd, &gm, NULL);
    line(150, 150, 450, 150);
    line(150, 200, 450, 200);
    line(150, 250, 450, 250);
    getch();     
    closegraph();
    return 0;
}
Last edited on
I cannot test your code because <graphics.h> is a non-standard header that I do not have. However, see this answer on StackOverflow, it sounds like it will fix your problem: https://stackoverflow.com/questions/31489097/graphics-h-c-undefined-reference-to-various-functions-like-line-initgraph
https://stackoverflow.com/a/42750895/8690169

If you don't use g++, tell us what compiler you do use.

BUT

Please note that graphics.h is an old BGI graphics library of the Turbo C DOS compiler. It is not meant for modern use. If you are interested in graphics, I highly suggest learning a clean, C++-oriented graphics library like SFML.
https://www.sfml-dev.org/index.php

If you still want to use graphics.h, and still can't get it to work, please consider Duthomhas' WinBGIm "fix", available on GitHub. Read through the README file to understand how it works.
https://github.com/Duthomhas/WinBGIm-fixed-sort-of-
Last edited on
I use DevC++ compiler
I use DevC++ Compiler.
DevC++ is g++, albeit probably an outdated version. You should be able to specify the commandline arguments in the compiler settings. g++ -o filename filename.cpp -lgraph

Did the first StackOverflow link fix your problem? If it didn't try using Duthomhas's WinBGIm fix.
Topic archived. No new replies allowed.