GLAD #defines 隐藏在 #ifndef GL_VERSION_X_X 后面,使用 Visual Studio 2022

问题描述 投票:0回答:1

我已经开始学习引擎开发,并决定在我的项目中使用 GLAD 和 SDL2。我遵循了各种教程,并成功地将 SDL2 和 GLAD 链接到我的项目。但我遇到了一个很奇怪的问题。

我尝试使用

glGenBuffers()
函数,但 Visual Studio 告诉我
glGenBuffers is undefined
。所以我进入glad.h并搜索
glGenBuffers
并在
#ifndef GL_VERSION_1_5
内部找到了它。 (我也无法使用任何其他功能。)

所以我尝试注释掉该部分的

#ifndef
#endif
,现在它成功了!我可以使用
glGenBuffers
。因为我知道我有 opengl 4.6 版本,所以我不必担心版本问题。

这里的问题是,这感觉好像我没有按照应有的方式去做,这可能会在将来带来问题。所以我所做的就是将

#ifndef
#endif
放回到原来的位置。但现在,glad.h 绝对充斥着这些类型的多个错误:

this declaration has no storage class or type specifier
the #if for this directive is missing
the #endif for this directive is missing

文档和我拿到时一模一样,但更奇怪的是现在其他功能都可以用了。即使那些不在

#ifndef GL_VERSION_1_5
中,但例如
#ifndef GL_VERSION_3_0

main.cpp 中的代码仍然编译并运行良好。当我折叠

#ifndef GL_VERSION_1_5
时,它现在也显示“活动预处理器块”,而其他人则显示“非活动预处理器块”。

我尝试用另一个副本替换该文档,现在我又回到了原来的问题,它指出

glGenBuffers is undefined
。值得注意的是
gladLoadGLLoader(( GLADloadproc )SDL_GL_GetProcAddress)
在运行时工作得很好,所以这不是 GLAD 本身的问题。

有人知道可能导致此问题的原因以及我应该如何解决这个问题吗?

我目前使用的包括:

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <glad/glad.h>

编译日志:

Build started...
1>------ Build started: Project: GrubEngineC++, Configuration: Debug x64 ------
1>GrubEngineC++.cpp
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\include\glad\glad.h(873,1): warning C4005: 'GL_INVALID_INDEX': macro redefinition
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\SDL2\include\SDL_opengl_glext.h(1651): message : see previous definition of 'GL_INVALID_INDEX'
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\include\glad\glad.h(915,1): warning C4005: 'GL_TIMEOUT_IGNORED': macro redefinition
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\SDL2\include\SDL_opengl_glext.h(1726): message : see previous definition of 'GL_TIMEOUT_IGNORED'
1>C:\Users\Arbeit\Desktop\C++ Engine\GEC\GrubEngineC++\GrubEngineC++.cpp(15,5): error C3861: 'glGenBuffers': identifier not found
1>Done building project "GrubEngineC++.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
c++ opengl sdl preprocessor glad
1个回答
1
投票

问题在于 OpenGL 定义的包含顺序。从编译日志中可以看出,还有其他定义,例如

GL_INVALID_INDEX
,当包含glad时已经定义了。

SDL_opengl.h
是SDL的OpenGL标头,已经包含了许多OpenGL相关符号的定义,并且与
glad.h
的使用发生冲突。您要么必须将其包含在
glad.h
之后,要么不包含全部(这更好,除非您需要
SDL_opengl.h
中的方法。

© www.soinside.com 2019 - 2024. All rights reserved.