Search code examples
c++opengllinker-errorsglu

OpenGL program failing to link?


I'm attempting to use the GA Sandbox source code found here on Linux (specifically Ubuntu 16.04). Yet when it comes to compiling the first example I'm left with this error.

g++  -g -O2   -o chap1ex1 chap1ex1.o  -lGL -lGLU -lglut ../../libgasandbox/libgasandbox.a
../../libgasandbox/libgasandbox.a(libgasandbox_a-gl_util.o): In function 
`pickLoadMatrix()':
/mnt/c/GASandbox/ga_sandbox-1.0.7/libgasandbox/gl_util.cpp:231: undefined 
reference to `gluPickMatrix'
collect2: error: ld returned 1 exit status
Makefile:216: recipe for target 'chap1ex1' failed

I should mention that the configure script which provided the Makefile mentioned above does not initially link either -lGL or -lGLU. This gave me an error pertaining to a missing DSO, which was corrected by linking -lGL. I then wound up with this error. I looked around for any similar errors online and found this post, where the OP solved it by linking -lGLU. I was not so fortunate.

Here is the snippet of code in question, from gl_util.cpp.

#include <string>

#include "gl_util.h"
#if defined (__APPLE__) || defined (OSX)
    #include <GLUT/glut.h>
#else
    #include <GL/glut.h>
#endif

// code inbetween

namespace GLpick {
bool g_pickActive = false;
int g_OpenGL_pick[4] = {0, 0, 0, 0}; 
double g_frustumNear = 1.0;
double g_frustumFar = 100.0;
double g_frustumWidth = -1.0;
double g_frustumHeight = -1.0;

int g_pickWinSize = 4;
}

void pickLoadMatrix() {
    if (!GLpick::g_pickActive) return;

    GLint viewport[4];
    glGetIntegerv(GL_VIEWPORT, viewport);
    gluPickMatrix(
        GLpick::g_OpenGL_pick[0], GLpick::g_OpenGL_pick[1], 
        GLpick::g_OpenGL_pick[2] * 2 + 1, GLpick::g_OpenGL_pick[3] * 2 + 1, 
        viewport);
}

In glu.h, gluPickMatrix()'s signature is GLAPI void GLAPIENTRY gluPickMatrix (GLdouble x, GLdouble y, GLdouble delX, GLdouble delY, GLint *viewport);. So, I attempted to change int g_OpenGL_pick[] = {0, 0, 0, 0}; to GLdouble g_OpenGL_pick[] = {0.0, 0.0, 0.0, 0.0};. Neither that nor casting the individual values to GLdouble worked.

What might I be overlooking? Or are there any concepts that could help narrow down my search?


Solution

  • Try moving the libgasandbox.a before all the -l options on the command line. So your command would look like this:

    g++ -g -O2 -o chap1ex1 chap1ex1.o ../../libgasandbox/libgasandbox.a -lGL -lGLU -lglut
    

    Order of arguments does matter for static linking, as described in this answer: things that depend on a library must come before that library. libgasandbox evidently depends on GLU, so putting it earlier should solve that error.

    You might also need to move -lGL to the very end, if GLU or glut depend on it (I'm not sure whether they do).