【问题标题】:Linking an openGL library fails because it can't open libgl.so ... but it's right there [closed]链接 openGL 库失败,因为它无法打开 libgl.so ......但它就在那里 [关闭]
【发布时间】:2018-05-23 02:37:10
【问题描述】:

当我尝试使用 openGL 制作 c++ 项目时,我遇到了以下错误,这对我来说真的难以捉摸。当我运行 make 文件时,我得到以下信息:

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

好的,当然。它没有找到图书馆。我将此行添加到我的 makefile 以进行调查

ld -lglut -lgl --verbose

除其他外,我得到以下信息

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

所以当我在网上查看时,似乎大多数这些错误都与 ld 没有在正确的目录中查找有关,但是 ld 正在查找正确的目录,因为这种尝试实际上针对的是正确的位置:

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

这是我正在使用的 makefile。

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

这里是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){
}

最后是 console.cpp:

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

【问题讨论】:

  • 不,您在该目录中没有该文件。你误会了。您确实有一个名为libGL.so 的文件,不幸的是,它不是链接器正在搜索的libgl.so 文件。细节很重要。
  • 我的错。我忘了linux有区分大小写的文件名。我用 -lGL 替换了 -lgl,它现在可以工作了。我非常感谢你,我不敢相信这花了我 3 个小时 lmao

标签: c++ opengl makefile g++ ld


【解决方案1】:

大多数 Linux 文件系统是 case-sensitive,因此当您在系统库目录中有 libGL.so 并且 ld 询问文件系统是否存在 libgl.so 时,它会说不存在并且链接将失败。

解决方案:确保您传递给 ld 的库名称与磁盘大小写匹配:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多