Wednesday 6 February 2019

c++ - Having many glut errors



I have found this code and wanted to try on my machine :



#include 


static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}

static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}



int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 01");


InitializeGlutCallbacks();

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glutMainLoop();

return 0;
}



And I got these errors :




g++ tutorial01.cpp



/tmp/ccOoXvqJ.o: In function `RenderSceneCB()':



tutorial01.cpp:(.text+0xa): undefined reference to `glClear'



tutorial01.cpp:(.text+0xf): undefined reference to `glutSwapBuffers'




/tmp/ccOoXvqJ.o: In function `InitializeGlutCallbacks()':



tutorial01.cpp:(.text+0x1f): undefined reference to `glutDisplayFunc'



/tmp/ccOoXvqJ.o: In function `main': tutorial01.cpp:(.text+0x43):



undefined reference to `glutInit' tutorial01.cpp:(.text+0x4d):



undefined reference to `glutInitDisplayMode'




tutorial01.cpp:(.text+0x5c): undefined reference to



`glutInitWindowSize' tutorial01.cpp:(.text+0x6b): undefined reference



to `glutInitWindowPosition' tutorial01.cpp:(.text+0x75): undefined



reference to `glutCreateWindow' tutorial01.cpp:(.text+0x8b): undefined



reference to `glClearColor' tutorial01.cpp:(.text+0x90): undefined




reference to `glutMainLoop' collect2: ld returned 1 exit status




I think I have successfully installed freeglut3-dev on my machine. Can you tell me why I'm getting so many errors? I'm using Ubuntu 12.04.


Answer



What you're seeing there are linker errors, i.e. the code your compiler processed was successfully translated into a compilation unit. Now the linker tries to make a exectuable and has trouble tieing up the loose ends of your compilation unit, namely the references to GLUT and OpenGL symbols. You have to tell the linker where else to look, than just your compilation units and the standard libraries.



Since your error messages look like what the GCC produces I suggest you add -lGL -lglut to your compiler's command line.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...