0
votes

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

#include <GL/freeglut.h>

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.

3
You don't just have to install glut, you also have to link with it's libraries. You should add something to your question about how you are building your program, since that's where the error is likely to be.john
Yes I had to add "-lglut". Thanks.jason

3 Answers

2
votes

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.

1
votes

These are linker errors for Glut and OpenGL. Linker is the software that tries to link all the object files together to get an exe.

You need to link both of them (OpenGl and Glut) with your project. Google has plenty of answers =)

1
votes

I downloaded the freeglut and compile the code in my Code::blocks using MinGW and i found that your problem is with the libraries, you need to link the libopengl32.a and the libfreeglut.a so that the program can run it.