openGL精湛的源代码编译错误

时间:2019-01-28 18:10:02

标签: c++ c opengl g++ glfw

我刚刚开始学习openGL Superbible的openGL编程。不幸的是,我无法编译任何示例。我在Ubuntu 18.10上,并使用官方存储库中的这些软件包准备了开发环境:

  • mesa-common-dev(通过这些软件包,您可以在/ usr / include / GL /中获得头文件)

  • libglfw3-dev(/ usr / include / GLFW /中的头文件)

这本书使用了一个小框架,在sb7.h标头(在书的源代码中)中有一个入口点,然后sb7.h指向glfw3和gl3w(后者随书的源代码一起提供,因此您不必不需要下载)。

这是这本书的第一个例子:

#ifndef _LINUX
#define _LINUX
#endif

// Include the "sb7.h" header file
#include "sb7.h"
// Derive my_application from sb7::application
class my_application : public sb7::application
{
public:
  // Our rendering function
  void render(double currentTime)
  {
    // Simply clear the window with red
    static const GLfloat red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 0, red);
  }
};
// Our one and only instance of DECLARE_MAIN
DECLARE_MAIN(my_application);

如果我尝试使用g++ main.cpp -lglfw对其进行编译(该文件称为“ main.cpp”),则会出现此错误:

/usr/bin/ld: /tmp/ccDOdloQ.o: in function `sb7::application::run(sb7::application*)':
main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x27e): undefined reference to `gl3wInit'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x29f): undefined reference to `gl3wIsSupported'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2af): undefined reference to `gl3wDebugMessageCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2bd): undefined reference to `sb7::application::debug_callback(unsigned int, unsigned int, unsigned int, unsigned int, int, char const*, void*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2c9): undefined reference to `gl3wEnable'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2de): undefined reference to `sb6IsExtensionSupported(char const*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2ee): undefined reference to `gl3wDebugMessageCallbackARB'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2fc): undefined reference to `sb7::application::debug_callback(unsigned int, unsigned int, unsigned int, unsigned int, int, char const*, void*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x308): undefined reference to `gl3wEnable'
/usr/bin/ld: /tmp/ccDOdloQ.o: in function `sb7::application::glfw_onResize(GLFWwindow*, int, int)':
main.cpp:(.text._ZN3sb711application13glfw_onResizeEP10GLFWwindowii[_ZN3sb711application13glfw_onResizeEP10GLFWwindowii]+0x15): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application13glfw_onResizeEP10GLFWwindowii[_ZN3sb711application13glfw_onResizeEP10GLFWwindowii]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: /tmp/ccDOdloQ.o: in function `sb7::application::glfw_onKey(GLFWwindow*, int, int, int, int)':
main.cpp:(.text._ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii[_ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii[_ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii]+0x23): undefined reference to `sb7::application::app'
/usr/bin/ld: /tmp/ccDOdloQ.o: in function `sb7::application::glfw_onMouseButton(GLFWwindow*, int, int, int)':
main.cpp:(.text._ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii[_ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii]+0x18): undefined reference to `sb7::application::app'
/usr/bin/ld: /tmp/ccDOdloQ.o:main.cpp:(.text._ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii[_ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii]+0x1f): more undefined references to `sb7::application::app' follow
/usr/bin/ld: /tmp/ccDOdloQ.o: in function `my_application::render(double)':
main.cpp:(.text._ZN14my_application6renderEd[_ZN14my_application6renderEd]+0x14): undefined reference to `gl3wClearBufferfv'
collect2: error: ld returned 1 exit status

在阅读消息时,我认为gl3w库有问题,因此我从github repository下载了该消息,并将其和glcorearb.h放在/usr/include/GL/中,指向#include在sb7.h中到该文件夹​​。最终,我编译了gl3w.c并使用g ++将其链接到main.cpp,如下所示:

  1. g++ -c gl3w.c
  2. g++ -c main.cpp
  3. g++ gl3w.o main.o

但是当我执行3)时,我得到其他错误:

/usr/bin/ld: gl3w.o: in function `open_libgl()':
gl3w.c:(.text+0x16): undefined reference to `dlopen'
/usr/bin/ld: gl3w.c:(.text+0x4d): undefined reference to `dlsym'
/usr/bin/ld: gl3w.o: in function `close_libgl()':
gl3w.c:(.text+0x6f): undefined reference to `dlclose'
/usr/bin/ld: gl3w.o: in function `get_proc(char const*)':
gl3w.c:(.text+0xc5): undefined reference to `dlsym'
/usr/bin/ld: main.o: in function `sb7::application::run(sb7::application*)':
main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x21): undefined reference to `glfwInit'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x7c): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x92): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0xb3): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0xd4): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0xe3): undefined reference to `glfwWindowHint'
/usr/bin/ld: main.o:main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0xf2): more undefined references to `glfwWindowHint' follow
/usr/bin/ld: main.o: in function `sb7::application::run(sb7::application*)':
main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x13f): undefined reference to `glfwGetPrimaryMonitor'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x172): undefined reference to `glfwCreateWindow'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x1c8): undefined reference to `glfwMakeContextCurrent'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x1e2): undefined reference to `glfwSetWindowSizeCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x1fc): undefined reference to `glfwSetKeyCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x216): undefined reference to `glfwSetMouseButtonCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x230): undefined reference to `glfwSetCursorPosCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x24a): undefined reference to `glfwSetScrollCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x279): undefined reference to `glfwSetInputMode'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2af): undefined reference to `gl3wDebugMessageCallback'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2bd): undefined reference to `sb7::application::debug_callback(unsigned int, unsigned int, unsigned int, unsigned int, int, char const*, void*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2c9): undefined reference to `gl3wEnable'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2de): undefined reference to `sb6IsExtensionSupported(char const*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2ee): undefined reference to `gl3wDebugMessageCallbackARB'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x2fc): undefined reference to `sb7::application::debug_callback(unsigned int, unsigned int, unsigned int, unsigned int, int, char const*, void*)'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x308): undefined reference to `gl3wEnable'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x339): undefined reference to `glfwGetTime'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x355): undefined reference to `glfwSwapBuffers'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x35a): undefined reference to `glfwPollEvents'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x372): undefined reference to `glfwGetKey'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x39b): undefined reference to `glfwWindowShouldClose'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x3e7): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application3runEPS0_[_ZN3sb711application3runEPS0_]+0x3ec): undefined reference to `glfwTerminate'
/usr/bin/ld: main.o: in function `sb7::application::glfw_onResize(GLFWwindow*, int, int)':
main.cpp:(.text._ZN3sb711application13glfw_onResizeEP10GLFWwindowii[_ZN3sb711application13glfw_onResizeEP10GLFWwindowii]+0x15): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application13glfw_onResizeEP10GLFWwindowii[_ZN3sb711application13glfw_onResizeEP10GLFWwindowii]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: main.o: in function `sb7::application::glfw_onKey(GLFWwindow*, int, int, int, int)':
main.cpp:(.text._ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii[_ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii]+0x1c): undefined reference to `sb7::application::app'
/usr/bin/ld: main.cpp:(.text._ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii[_ZN3sb711application10glfw_onKeyEP10GLFWwindowiiii]+0x23): undefined reference to `sb7::application::app'
/usr/bin/ld: main.o: in function `sb7::application::glfw_onMouseButton(GLFWwindow*, int, int, int)':
main.cpp:(.text._ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii[_ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii]+0x18): undefined reference to `sb7::application::app'
/usr/bin/ld: main.o:main.cpp:(.text._ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii[_ZN3sb711application18glfw_onMouseButtonEP10GLFWwindowiii]+0x1f): more undefined references to `sb7::application::app' follow
/usr/bin/ld: main.o: in function `my_application::render(double)':
main.cpp:(.text._ZN14my_application6renderEd[_ZN14my_application6renderEd]+0x14): undefined reference to `gl3wClearBufferfv'
collect2: error: ld returned 1 exit status

在这一点上,我尝试了gl3w.h的single file version,其中已经包含gl3w.c。我将两个标头放在/usr/include/GL/中,然后将sb7.h指向该版本的库,但是g++ main.cpp -lglfw出现了其他错误:

In file included from main.cpp:6:
sb7.h: In member function ‘virtual void sb7::application::run(sb7::application*)’:
sb7.h:137:9: error: ‘gl3wInit’ was not declared in this scope
         gl3wInit();
         ^~~~~~~~
sb7.h:137:9: note: suggested alternative: ‘glfwInit’
         gl3wInit();
         ^~~~~~~~
         glfwInit
sb7.h:147:17: error: ‘gl3wIsSupported’ was not declared in this scope
             if (gl3wIsSupported(4, 3))
                 ^~~~~~~~~~~~~~~
sb7.h:147:17: note: suggested alternative: ‘gl3w_is_supported’
             if (gl3wIsSupported(4, 3))
                 ^~~~~~~~~~~~~~~
                 gl3w_is_supported

我还在#define GL3W_IMPLEMENTATION之前添加了#include "GL/gl3w.h",如gl3w单个文件版本的文档中所建议,但是没有运气。 我在另一本书的源代码中也遇到了同样的错误。

希望有人可以帮助我。 谢谢。

0 个答案:

没有答案