7
#include<graphics.h>
#include<stdio.h>

main()
{
int gd=DETECT,gm;
int i,x,y;
initgraph(&gd,&gm,"");
line(0,0,640,0);
line(0,0,0,480);
line(639,0,639,480);
line(639,479,0,479);
for(i=0;i<=1000;i++)
{
   x=rand()%639;
   y=rand()%480;
   putpixel(x,y,15);
}
  getch();
  closegraph();
}

The Following is a Basic Graphic Program,It Shows the Errors as

  1. undefined reference to 'initgraph'

  2. undefined reference to 'closegraph'

  3. undefined reference to 'line'[4 times]

  4. undefined reference to 'putpixel'

    Compiler : CodeBlocks; Language:c++;


I Have Copied the graphics.h and winbgim.h in include folder and the libbgi.a in the lib folder also i have linked all the libraries required to be linked. Please Help.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Adit Jain
  • 151
  • 2
  • 3
  • 6
  • I think that this question has already been answered : [http://stackoverflow.com/questions/20313534/how-to-use-graphics-h-in-codeblocks][1] [1]: http://stackoverflow.com/questions/20313534/how-to-use-graphics-h-in-codeblocks – Been Forgotten Jul 18 '15 at 08:12
  • 1
    @BeenForgotten No That Is What I Stated, My Graphics.h file is in the write pace and all the linking has been done , but still it shows undefined reference to.. . – Adit Jain Jul 18 '15 at 09:23
  • in what namespace are these functions defined? – 463035818_is_not_an_ai Jul 18 '15 at 09:49
  • How Do I Find That @tobi303 – Adit Jain Jul 18 '15 at 12:37
  • I think `#include` is the problem. Do not know what compiler/IDE you are using but some have their own `graphics.h` lib that is not BGI so my bet is you are including different lib then you think you are. try `#include "graphics.h"` or #include "drive:\\...path..\\graphics.h" with correct and full path to avoid problems... To check for correct include try to open the file from the IDE if you can (some have that feature just put cursor in the include line inside filename and either right click an use popup menu or hit the shortcut for it like `ALT+Enter`,`CTRL+Enter` or `F3`) – Spektre Jul 20 '15 at 05:53

2 Answers2

6

Change

initgraph(&gd,&gm,"");

to

initgraph(&gd,&gm,NULL);

for compiling:

g++ -o filename filename.cpp -lgraph

to execute:

./filename
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Tushar Nitave
  • 519
  • 4
  • 13
  • 1
    Not working for me. I get an error. C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lgraph collect2.exe: error: ld returned 1 exit status. Please help me. – AurumTechie Jul 03 '21 at 14:42
0

The functions in graphics.h are only supported by old ancient Turbo C and Turbo C++ compilers. Those functions can not be used by any modern 32-bit compiler. You can either get a copy of that old MS-DOS compiler or use win32 api GDI functions or get one of several graphics libraries such as OpenGL and QT.

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68