Reputation: 21
I am trying to use graphics.h in dev C++ 5.7.1.
I already searched the internet for the available options. I downloaded the graphics.h library in the include folder and chose the following in the parameters option:
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
Still I cannot figure out why is it showing me these errors:
undefined reference to `initgraph'
undefined reference to `graphresult'
undefined reference to `grapherrormsg'
undefined reference to `getmaxx'
undefined reference to `getmaxy'
undefined reference to `getmaxcolor'
undefined reference to `getcolor'
undefined reference to `circle'
undefined reference to `closegraph'
[Error] ld returned 1 exit status.
This is my code:
#include<iostream>
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
//#include<conio>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int midx, midy;
int radius = 100;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:\\Program Files (x86)\\Dev-Cpp\\MinGW64\\include\\winbgim");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
midx = getmaxx() / 2;
midy = getmaxy() / 2;
setcolor(getmaxcolor());
/* draw the circle */
circle(midx, midy, radius);
/* clean up */
getch();
closegraph();
return 0;
}
Upvotes: 2
Views: 26205
Reputation: 1
This is linker error. Make sure library names you wrote in Project Options->Parameters->Linker start with "-".
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
as Zeke wrote above.
Upvotes: 0
Reputation: 8879
Download following files to the directories mentioned:
Here I assume you installed Dev-Cpp to C:\Dev-Cpp
http://www.cs.colorado.edu/~main/bgi/dev-c++/graphics.h Directory:> C:\Dev-Cpp\include http://www.cs.colorado.edu/~main/bgi/dev-c++/libbgi.a Directory:> C:\Dev-Cpp\lib
Create a new C++ project and set "Project Options->Parameters->Linker" as
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32
and try executing this sample code; then go for the code that you posted above.
#include<graphics.h>
int main( ){
initwindow( 700 , 700 , "MY First Program");
circle(200, 200, 150);
getch();
return 0;
}
Upvotes: 3
Reputation: 426
The error messages indicate that the function is not defined. Functions are typically defined in the source code or a library (static or dynamic.) Therefore, you need to either include the source code in your project so that it's linked along with your code above, or you will need to link to the precompiled library (perhaps an .a, .lib, or .dll file.)
I'm not overly familiar with the library you're using; however, I would assume that graphics.h includes the function declarations and does not include function definitions.
Upvotes: 0