【发布时间】:2020-10-25 10:58:18
【问题描述】:
我正在学习如何将 OpenGL 与 C++ 一起使用,我正在学习open.gl 上的教程。我在教程中的this 部分,我正在使用 GLFW。我尝试按照说明构建 GLFW,
从网站下载 GLFW 二进制包后,或 自己编译库,你会在包含的头文件中找到 文件夹和用于编译器的库位于其中一个 lib 文件夹中。
-将适当的 lib 文件夹添加到您的库路径并与 GLFW 链接。
-将包含文件夹添加到您的包含路径。
我下载了它,并将包含文件夹移动到我的 MinGW 包含文件夹中,但我不确定在哪里可以找到 lib 文件夹,如何将它添加到我的库路径,或者如何将它与 OpenGL 链接。这在教程中没有解释,我在网上找不到任何关于这意味着什么或如何做的说明。我没有做其他两个步骤就继续了,当尝试运行教程中的测试代码时:
#include <GLFW/glfw3.h>
#include <thread>
int main()
{
glfwInit();
std::this_thread::sleep_for(std::chrono::seconds(1));
glfwTerminate();
}
我收到了这个错误:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\thread:35:0,
from D:\Files\Documents\Programming\opengl_tut.cpp:2:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
我将 -std=c++11 添加到我的编译器标志中,我收到了这个错误:
D:\Files\Documents\Programming\opengl_tut.cpp: In function 'int main()':
D:\Files\Documents\Programming\opengl_tut.cpp:7:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
认为问题可能出在 std::this_thread 上,我从程序中删除了该行,然后运行它以查看初始化和终止 GLFW 是否会返回任何错误。我得到了这个:
C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o:opengl_tut.cpp:(.text+0xc): undefined reference to `glfwInit'
C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o:opengl_tut.cpp:(.text+0x11): undefined reference to `glfwTerminate'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\Brett\AppData\Local\Temp\ccCJrDGj.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
我认为这可能是因为我跳过了教程的前面部分。
TL;DR
- 尝试按照教程使用 OpenGL
- 无法弄清楚如何链接 GLFW
- 找不到 lib 文件夹
- 遇到与 std::this_thread 相关的错误
- 遇到与 GLFW 相关的错误
编辑
我在 GLFW 网站上发现了一些信息说要使用 Cmake。我下载了它并尝试使用它从下载的文件中构建。它创建了一个新文件夹,但我不确定如何处理它。
【问题讨论】:
-
最后是链接器错误。它告诉您必须将您的程序与适当的 glfw 库链接。 Look here
-
从不将任何东西(头文件、库等)安装到编译器工具链的安装目录中。始终为第三方库(头文件、源代码、二进制文件)创建单独的目录树,并将该目录配置为编译器和链接器搜索路径的一部分。
-
为什么?我已经把我的头文件和我的编译器放在了同一个目录中。
-
@Bretsky 当您以后需要更新编译器或 OpenGL 时,将它们放在不同的位置会更容易,例如删除旧目录并重新安装新版本。
-
@Bretsky 你有制作文件吗?