Search code examples
c++openglrenderfreeglut

OpenGL: Cannot perform rendering with freeGLUT? C++


I just started out with freeGLUT today, and I have gotten everything installed nicely. I am able to create a basic window but whenever I call almost every rendering method, the compiling fails. I am using the following code:

#include "math_lib.h"
#include <stdlib.h>
#include <GL/freeglut.h>

void render(void);

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(640, 480);
    glutCreateWindow("Simple GLUT Application");

    glutDisplayFunc(render);    

    glutMainLoop();
}

void render(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
        glColor3f(1, 0, 0);
        glVertex2f(-0.5, -0.5);
        glColor3f(0, 1, 0);
        glVertex2f(0.5, -0.5);
        glColor3f(0, 0, 1);
        glVertex2f(0.0, 0.5);
    glEnd();

    glutSwapBuffers();
}

And the error I get is:

C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:29: undefined reference to `glClear@4'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:31: undefined reference to `glBegin@4'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:32: undefined reference to `glColor3f@12'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:33: undefined reference to `glVertex2f@8'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:34: undefined reference to `glColor3f@12'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:35: undefined reference to `glVertex2f@8'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:36: undefined reference to `glColor3f@12'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:37: undefined reference to `glVertex2f@8'
C:\Users\User\Dropbox\NetBeans Workspace\OpenGL_Testing/main.cpp:38: undefined reference to `glEnd@0'

So am I doing something wrong or am I just missing an import?


Solution

  • By the error message it looks like Windows. You need to link to opengl32.lib.