Search code examples
c++openglmakefileg++ld

Linking an openGL library fails because it can't open libgl.so ... but it's right there


I've been getting the following error when I try to make a c++ project with openGL that's been really elusive to me. When i run the make file, I get the following:

g++ -c init.cpp
g++ -o executable console.o init.o -lglut -lgl
/usr/bin/x86_64-linux-gnu-ld: cannot find -lgl
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 1

okay, sure. it's not finding the library. I added this line to my makefile to investigate

ld -lglut -lgl --verbose

And I get the following, amoungst other things

attempt to open //usr/local/lib/x86_64-linux-gnu/libglut.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libglut.a failed
attempt to open //lib/x86_64-linux-gnu/libglut.so failed
attempt to open //lib/x86_64-linux-gnu/libglut.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libglut.so succeeded
-lglut (//usr/lib/x86_64-linux-gnu/libglut.so)
attempt to open //usr/local/lib/x86_64-linux-gnu/libgl.so failed
attempt to open //usr/local/lib/x86_64-linux-gnu/libgl.a failed
attempt to open //lib/x86_64-linux-gnu/libgl.so failed
attempt to open //lib/x86_64-linux-gnu/libgl.a failed
attempt to open //usr/lib/x86_64-linux-gnu/libgl.so failed
attempt to open //usr/lib/x86_64-linux-gnu/libgl.a failed
attempt to open //usr/lib/x86_64-linux-gnu64/libgl.so failed
attempt to open //usr/lib/x86_64-linux-gnu64/libgl.a failed
attempt to open //usr/local/lib64/libgl.so failed
attempt to open //usr/local/lib64/libgl.a failed
attempt to open //lib64/libgl.so failed
attempt to open //lib64/libgl.a failed
attempt to open //usr/lib64/libgl.so failed
attempt to open //usr/lib64/libgl.a failed
attempt to open //usr/local/lib/libgl.so failed
attempt to open //usr/local/lib/libgl.a failed
attempt to open //lib/libgl.so failed
attempt to open //lib/libgl.a failed
attempt to open //usr/lib/libgl.so failed
attempt to open //usr/lib/libgl.a failed
attempt to open //usr/x86_64-linux-gnu/lib64/libgl.so failed
attempt to open //usr/x86_64-linux-gnu/lib64/libgl.a failed
attempt to open //usr/x86_64-linux-gnu/lib/libgl.so failed
attempt to open //usr/x86_64-linux-gnu/lib/libgl.a failed
ld: cannot find -lgl
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 1

So when I looked online it seemed like most of these errors had to do with ld not looking in the right directory, however ld IS looking in the right directory because this attempt actually targets the right location:

attempt to open //usr/lib/x86_64-linux-gnu/libgl.so failed

Not only do I have the file in that directory, but it was last read from at 8:00, 3 hours ago because I was able to link it earlier somehow even though I haven't changed anything related to linking

Here's the makefile i'm using.

cc=g++

edit: init.o console.o
    ld -lglut -lgl --verbose
    $(cc) -o executable console.o init.o -lglut -lgl

init.o: init.cpp
    $(cc) -c init.cpp

console.o: console.cpp
     $(cc) -c console.cpp

clean:
    rm init.o console.o executable

And here is init.cpp:

#include <cstdlib>
//#include </usr/include/GL/glew.h>
#include <GL/freeglut.h>
//#include <GL/gl.h>
void consoleDisplay(int a);
void renderCB(void);
void resizeCB(int W, int H);
void idleCB(void);
void timerCB(int Value);
void glutInitErrorFunc(void (*callback)(const char *fmt, va_list ap) );
void glutInitWarningFunc(void (*callback)(const char *fmt, va_list ap) );
int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitContextVersion(4, 0);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(300, 300);
    int windowLeft= glutGet(GLUT_WINDOW_X);
    int windowWidth= glutGet(GLUT_WINDOW_WIDTH);
    consoleDisplay(windowLeft);
    consoleDisplay(windowWidth);
    int winID = glutCreateWindow("Hello again, world.");
    glutReshapeFunc(resizeCB);
    glutDisplayFunc(renderCB);
    glutIdleFunc(idleCB);
    glutTimerFunc(0, timerCB ,0);
    glutMainLoop();
    return 0;
}
void renderCB(void){
    //doActualOpenGLStuffHere()
    glutSwapBuffers();
    glutPostRedisplay();
}

void resizeCB(int W, int H){
    glViewport(0, 0, W, H);
}

void idleCB(void){
    glutPostRedisplay();
}

void timerCB(int Value){
}

And lastly console.cpp:

#include <iostream>
void consoleDisplay(int a){
    std::cout << a << "\n";
}

Solution

  • Most Linux filesystems are case-sensitive so when you have a libGL.so in the system library directory and ld asks the filesystem if libgl.so exists there it will say no and linking will fail.

    Solution: make sure the library names you're passing to ld match the on-disk capitalization:

    $(cc) -o executable console.o init.o -lglut -lGL
                                                  ^^ note the caps