问题
I want to compile a GLUT program
#include <GL/glut.h>
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Lighthouse3D- GLUT Tutorial");
return 1;
}
Compile and link command:
gcc -o main.exe main.c -lglut32
Result:
main.o:main.c:(.text.startup+0x1c): undefined reference to `glutInit'
main.o:main.c:(.text.startup+0x28): undefined reference to `glutInitDisplayMode'
main.o:main.c:(.text.startup+0x3c): undefined reference to `glutInitWindowPosition'
main.o:main.c:(.text.startup+0x50): undefined reference to `glutInitWindowSize'
main.o:main.c:(.text.startup+0x5c): undefined reference to `glutCreateWindow'
collect2: ld returned 1 exit status
The actual lib file of glut (3.7.6, called glut32.lib) is in the lib folder of mingw and the include file in include/GL.
What to do now?
回答1:
Try it with libglut.a
and libglut32.a
instead of glut32.lib
. To compile glut programs with minGW.
Do GLUT for Win32 is a Windows port of the original GLUT library. It’s no longer maintained or supported. The MinGW “w32api” package already comes with two GLUT import libraries, “libglut.a” and “libglut32.a”, but doesn’t come with a glut header file.
If you’ve ever downloaded a GLUT header from the internet, and attempted to link an application against either of these import libraries, you likely would have seen it fail with various undefined references.
Setting Up GLUT for Win32 With MinGW Look for Setting Up GLUT for Win32 With MinGW
If you want to have your own "build" of libglut32.a and glu32.dll.
- Download Source glut-3.7.6-src.zip (4.76 MB)
- Download makefile
- Make the following modifications:
Add the following lines to include/GL/glut.h starting at line 12:
#ifdef __MINGW32__
#define APIENTRY __stdcall
#define CALLBACK __stdcall
#endif
Comment out line 21 in lib/glut/win32_winproc.c so that it looks:
//#include <crtdbg.h>
makefile line 5 replace:
-enable-auto-import with --enable-auto-import
run make
Have tried it with the example.c file from the guidance in the link Setting Up GLUT for Win32 With MinGW
.
With the prebuild libs and the self compiled libs. Works both.
below is the result.
Example GLUT Application
example.c from the link above Setting Up GLUT for Win32 With MinGW
.
#include <stdlib.h>
#include <GL/glut.h>
void keyboard(unsigned char key, int x, int y);
void display(void);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutCreateWindow("GLUT Test");
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush();
}
来源:https://stackoverflow.com/questions/12919277/glut-program-link-error