1

I have a program that worked on my computer before I wiped the hard drive and reinstalled the operating system. I reinstalled all the necessary compilers and DLLs but im getting linker errors for glut. below is a snippet of the code I am trying to run and what my compile command is.

Here is the main I am trying to run:

  /*
 * GL01Hello.cpp: Test OpenGL C/C++ Setup
 */
#include <windows.h>  // For MS Windows
#include <GL/freeglut.h>  // GLUT, includes glu.h and gl.h

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void display() {
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
   glClear(GL_COLOR_BUFFER_BIT);         // Clear the color buffer

   // Draw a Red 1x1 Square centered at origin
   glBegin(GL_QUADS);              // Each set of 4 vertices form a quad
      glColor3f(1.0f, 0.0f, 0.0f); // Red
      glVertex2f(-0.5f, -0.5f);    // x, y
      glVertex2f( 0.5f, -0.5f);
      glVertex2f( 0.5f,  0.5f);
      glVertex2f(-0.5f,  0.5f);
   glEnd();

   glFlush();  // Render now
}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
   glutInit(&argc, argv);                 // Initialize GLUT
   glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
   glutInitWindowSize(320, 320);   // Set the window's initial width & height
   glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
   glutDisplayFunc(display); // Register display callback handler for window re-paint
   glutMainLoop();           // Enter the infinitely event-processing loop
   return 0;
}

And here is the command that is being run:

g++ GL01Hello.cpp -o GL01Hello.exe -lglu32 -lopengl32 -lfreeglut

and the errors I am getting are all below:

g++ GL01Hello.cpp -o GL01Hello.exe -lglu32 -lopengl32 -lfreeglut
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit@12'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x3e): undefined reference to `_imp____glutCreateWindowWithExit@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x60): undefined reference to `_imp____glutCreateMenuWithExit@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x198): undefined reference to `_imp__glutInitWindowSize@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1b1): undefined reference to `_imp__glutInitWindowPosition@8'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1c2): undefined reference to `_imp__glutDisplayFunc@4'
C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o:GL01Hello.cpp:(.text+0x1cc):
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\Daniel\AppData\Local\Temp\ccC01yPe.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Makefile:2: recipe for target 'all' failed
make: *** [all] Error 1

Any help in resolving these linker errors would be extremely helpful.

Edit: I edited all the code above to be a very simple example, and I am still getting a very similar linker error. Someone reported this as a similar problem to the generic "what causes linker errors" Post and I have already tried many of those and none of them have helped. Thank you fo the attempt though

Daniel
  • 43
  • 4
  • Most likely the problem is not in just your main function. If I had to make a guess it's something about the order in which you include `glut.h`, `glew.h` and – since there's a linker error about it – ' glm.h'. Both GLUT and GLEW are doing quite funky stuff with macros to avoid namespace pollution. But it can also trip up linking badly if things get tangled. Try to reduce your code to a minimal example that still exposes the problem and post the full code of that. – datenwolf Jul 14 '15 at 07:59
  • Thank you for your response. I changed to code to a very simple example. any help would be appreciated. – Daniel Jul 15 '15 at 00:40

1 Answers1

0

Well, everything you do looks right. My best bet is, that the build of FreeGLUT you're using (specifically the .def and .lib linker stubs) don't match (possibly also the .dll). My suggestion would be, to build FreeGLUT from sources using the very same compiler you're also using to build your program. Try that, and report on any differences.

datenwolf
  • 159,371
  • 13
  • 185
  • 298