Question

I'm using Codeblocks (in Ubuntu with GCC) and have apt-gotten the necessary files for OpenGL and am now going through a tutorial on the basics of OpenGL:

My (tutorial based) code:

#include <GL/glu.h>
#include <GL/freeglut.h>

//#include <GL/gl.h>
#include <GL/glut.h>

void display(void)
{
    /* clear window */

     glClear(GL_COLOR_BUFFER_BIT);


    /* draw unit square polygon */

    glBegin(GL_POLYGON);
        glVertex2f(-0.5, -0.5);
        glVertex2f(-0.5, 0.5);
        glVertex2f(0.5, 0.5);
        glVertex2f(0.5, -0.5);
    glEnd();

    /* flush GL buffers */

    glFlush();

}


void init()
{

    /* set clear color to black */

    /*  glClearColor (0.0, 0.0, 0.0, 0.0); */
    /* set fill  color to white */

    /*  glColor3f(1.0, 1.0, 1.0); */

    /* set up standard orthogonal view with clipping */
    /* box as cube of side 2 centered at origin */
    /* This is default view and these statement could be removed */

    /* glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);  */
}

int main(int argc, char** argv)
{

    /* Initialize mode and open a window in upper left corner of screen */
    /* Window title is name of program (arg[0]) */

    /* You must call glutInit before any other OpenGL/GLUT calls */
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(0,0);
    glutCreateWindow("simple");
    glutDisplayFunc(display);
    init();
    glutMainLoop();

}

When I try to run this in Codeblocks (with GCC set in the build settings) I get the following error:

error: undefined reference to glClear error:

undefined reference to glBegin ...

After trying for hours to change build settings, I decided to just call GCC from the command line: gcc -o main main.c -lGL -lGLU -lglut

This compiles without problem and I can even run it. So I'm not sure what the problem is. (Also, in Codeblocks I did add "-lGL -lGLU -lglut" to the build options.)

Why can command line GCC compile this but codeblocks can't?

Note: In Codeblocks I turned off all compiler flags in build settings. Linker Settings is empty. The only thing I have added are the above-mentioned compiler options.

Was it helpful?

Solution

Linker Settings is empty. The only thing I have added are the above-mentioned compiler options

And that's the problem right there: Library linkage is a linker setting. Those -l… must go into linker settings, not compiler settings. Also it's important to add gdi32.lib before opengl32.lib, so that all symbols can be found.

On Windows the OpenGL headers themself depend on a macro defined in windows.h so you should write something like

#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top