【问题标题】:OpenGL/glut/stdc++ build errorsOpenGL/glut/stdc++ 构建错误
【发布时间】:2011-07-21 14:47:31
【问题描述】:

我正在使用 Ubuntu 并尝试使用 synaptic 安装所有包含“GLUT”一词的所有内容以及 SDL 和 opengl 。但是一个简单的程序仍然无法编译。它显示了这一点:

opengl1.cpp:(.text+0xe): undefined reference to `glClear' opengl1.cpp:(.text+0x1a): undefined reference to `glBegin' opengl1.cpp:(.text+0x2e): undefined reference to `glVertex2i' opengl1.cpp:(.text+0x33): undefined reference to `glEnd' opengl1.cpp:(.text+0x38): undefined reference to `glFlush' /tmp/ccnwQeLu.o:在函数“MyInit()”中: opengl1.cpp:(.text+0x4c): 对“glGetString”的未定义引用 opengl1.cpp:(.text+0x57): undefined reference to `std::cout' opengl1.cpp:(.text+0x5c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' opengl1.cpp:(.text+0x6c): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' opengl1.cpp:(.text+0x78): undefined reference to `glGetString' opengl1.cpp:(.text+0x83): undefined reference to `std::cout' opengl1.cpp:(.text+0x88): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, unsigned char const*)' opengl1.cpp:(.text+0x98): undefined reference to `std::basic_ostream >& std::operator >(std::basic_ostream >&, char const*)' opengl1.cpp:(.text+0xc0): undefined reference to `glClearColor' opengl1.cpp:(.text+0xdf): undefined reference to `glColor3f' opengl1.cpp:(.text+0xec): undefined reference to `glPointSize' opengl1.cpp:(.text+0xf8): undefined reference to `glMatrixMode' opengl1.cpp:(.text+0xfd): undefined reference to `glLoadIdentity' opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D' /tmp/ccnwQeLu.o:在函数“main”中: opengl1.cpp:(.text+0x14a): undefined reference to `glutInit' opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize' opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition' opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow' opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc' opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop' /tmp/ccnwQeLu.o:在函数“__static_initialization_and_destruction_0(int, int)”中: opengl1.cpp:(.text+0x1cb): 未定义引用`std::ios_base::Init::Init()' opengl1.cpp:(.text+0x1d0): 未定义引用`std::ios_base::Init::~Init()' /tmp/ccnwQeLu.o:(.eh_frame+0x12): 未定义对 `__gxx_personality_v0' 的引用 collect2: ld 返回 1 个退出状态

如果我使用 g++ 而不是 gcc,我会得到:

vim opebgl1.cpp g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm 然后得到这个: /tmp/ccCJBuIl.o:在函数“MyInit()”中: opengl1.cpp:(.text+0x12d): undefined reference to `gluOrtho2D' /tmp/ccCJBuIl.o:在函数“主”中: opengl1.cpp:(.text+0x14a): undefined reference to `glutInit' opengl1.cpp:(.text+0x156): undefined reference to `glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to `glutInitWindowSize' opengl1.cpp:(.text+0x181): undefined reference to `glutInitWindowPosition' opengl1.cpp:(.text+0x18d): undefined reference to `glutCreateWindow' opengl1.cpp:(.text+0x19e): undefined reference to `glutDisplayFunc' opengl1.cpp:(.text+0x1a3): undefined reference to `glutMainLoop' collect2: ld 返回 1 个退出状态

那么我真正需要什么才能开始在 Ubuntu 中使用 opengl?

【问题讨论】:

  • 您在构建应用程序时是否链接了所有适当的库?
  • 我包含了这些头文件:#include #include
  • 另外,您是使用g++ 还是gcc 构建的?
  • 包含头文件只是您需要做的一半。这允许程序编译但不能链接。最终创建您的应用程序(或库)的命令应该链接到您使用的库,否则它将不知道从哪里获取所有这些符号。发布您的构建命令,我们可以为您提供一些指导。
  • 使用 g++ 构建 C++ 源代码。你会发现与std::cout 之类的错误有关的错误就会消失。 g++ 实际上只是 gcc 的一个包装器,它会自动为您链接到标准 C++ 库。如果您使用gcc,则必须手动进行。使用g++ 要容易得多。

标签: c++ opengl linker-errors glut


【解决方案1】:

将“-lstdc++ -lGL”添加到您的链接器标志。 或者尝试像这样编译它:

g++ opengl1.cpp -o opengl1 -lGL -lGLU -lc -lm

(编辑:添加 -lGLU,删除 -lstdc++)

【讨论】:

  • 现在 im getting /tmp/ccCJBuIl.o: In function MyInit()': opengl1.cpp:(.text+0x12d): undefined reference to gluOrtho2D' /tmp/ccCJBuIl.o: In function main': opengl1.cpp:(.text+0x14a): undefined引用glutInit' opengl1.cpp:(.text+0x156): undefined reference to glutInitDisplayMode' opengl1.cpp:(.text+0x16d): undefined reference to opengl1.cpp:(.text+0x181): undefined reference to glutInitWindowPosition' opengl1.cpp:(.text+0x19e): undefined reference to glutDisplayFunc' opengl1.cpp:(.text+0x1a3) : undefined reference to `glutMainLoop' collect2: ld returned 1 exit status
  • @user: 在 -lGL 的左边添加 -lglut 和 -lGLU。
  • 如果您使用的是g++,为什么还需要-lstdc++?看来问题是@user553492 错误地使用了gcc
  • @pelya: -lstdc++ 在您使用 g++ 编译/链接时是隐式的(除非您明确禁用它)
  • 错误似乎与使用 g++ 有关。现在唯一的问题是程序可以编译,但是当我运行它时,它说找不到命令。
【解决方案2】:

您也应该将其与 glut 和 GLU 链接:

g++ opengl1.cpp -o opengl1 -lGL -lstdc++ -lc -lm -lglut -lGLU

.cpp 结尾的文件必须使用 g++ 编译。其余的错误与链接过程有关,如果您使用我上面建议的命令构建应用程序,它们不应该发生。如果有,请确保您已安装 libglutlibglu

【讨论】:

    【解决方案3】:

    确保您使用 g++ 构建您的 C++ 文件,以避免与 C++ 标准库相关的链接器错误。

    要解决 GLUT 符号,您可能只需将 -lglut 添加到最终构建命令中。

    【讨论】:

      【解决方案4】:

      在编译代码时,您必须根据包含的内容提及一些参数。 您已经使用过-lGL-lm 等。 要使用gluOrtho2D,要使用的参数是-lGLU 对于以glut 开头的其他函数,请使用-lglut

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-22
        • 1970-01-01
        • 2016-07-09
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        相关资源
        最近更新 更多