如何修复以下 gcc 警告?
我已经开始学习 OpenGL 并设法使用顶点缓冲区对象创建一个旋转立方体。但是,当我编译代码时,gcc 会发出以下警告:
|| sdlogl.c: In function ‘initGL’:
sdlogl.c|48| warning: implicit declaration of function ‘glGenBuffers’
sdlogl.c|50| warning: implicit declaration of function ‘glBindBuffer’
sdlogl.c|51| warning: implicit declaration of function ‘glBufferData’
我应该做什么来修复这些警告?
这是我的代码:
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
SDL_Surface *surface;
float rquad=0.0f;
GLuint vertexBuffer,colorBuffer;
float vert[]={
1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1,
1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1,
1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1,
-1, 1, 1, -1,-1, 1, -1,-1,-1, -1, 1,-1,
1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1,
1, 1,-1, -1, 1,-1, -1,-1,-1, 1,-1,-1
};
float colors[]={
1,0,0, 1,0,0, 1,0,0, 1,0,0,
0,1,0, 0,1,0, 0,1,0, 0,1,0,
0,0,1, 0,0,1, 0,0,1, 0,0,1,
1,1,0, 1,1,0, 1,1,0, 1,1,0,
1,0,1, 1,0,1, 1,0,1, 1,0,1,
0,1,1, 0,1,1, 0,1,1, 0,1,1
};
Uint32 timerfunc(Uint32 interval,void* param){
rquad+=0.8f;
return interval;
}
void initGL(){
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,640.0/480.0,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH);
glClearColor(0,0,0,0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glGenBuffers(1,&vertexBuffer);
glGenBuffers(1,&colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,72*sizeof(float),vert,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,colorBuffer);
glBufferData(GL_ARRAY_BUFFER,72*sizeof(float),colors,
GL_STATIC_DRAW);
}
void drawGLScene(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-6);
glRotatef(rquad,1,1,0.5);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glVertexPointer(3,GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER,colorBuffer);
glColorPointer(3,GL_FLOAT,0,0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS,0,24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
SDL_GL_SwapBuffers();
}
int main(int argc, char** argv){
int videoFlags=0;
videoFlags|=SDL_OPENGL;
videoFlags|=SDL_GL_DOUBLEBUFFER;
videoFlags|=SDL_HWPALETTE;
videoFlags|=SDL_HWSURFACE;
videoFlags|=SDL_HWACCEL;
int done=0;
SDL_Event event;
const SDL_VideoInfo* videoInfo;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
videoInfo=SDL_GetVideoInfo();
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDL_WM_SetCaption("Hello OpenGL!",NULL);
surface=SDL_SetVideoMode(640,480,32,videoFlags);
initGL();
SDL_AddTimer(10,timerfunc,NULL);
while(!done){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
done=1;
}
}
drawGLScene();
}
SDL_Quit();
return 0;
}
此外,我使用以下命令编译代码:gcc sdlogl.c -g -Wall -lSDL -lGL -lGLU
I've started learning OpenGL and managed to create a spinning cube using vertex buffer objects. However, when I compile my code, gcc issues the following warnings:
|| sdlogl.c: In function ‘initGL’:
sdlogl.c|48| warning: implicit declaration of function ‘glGenBuffers’
sdlogl.c|50| warning: implicit declaration of function ‘glBindBuffer’
sdlogl.c|51| warning: implicit declaration of function ‘glBufferData’
What should I do to fix these warnings?
This is my code:
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
SDL_Surface *surface;
float rquad=0.0f;
GLuint vertexBuffer,colorBuffer;
float vert[]={
1, 1, 1, 1, 1,-1, -1, 1,-1, -1, 1, 1,
1,-1, 1, 1,-1,-1, -1,-1,-1, -1,-1, 1,
1, 1, 1, 1,-1, 1, 1,-1,-1, 1, 1,-1,
-1, 1, 1, -1,-1, 1, -1,-1,-1, -1, 1,-1,
1, 1, 1, -1, 1, 1, -1,-1, 1, 1,-1, 1,
1, 1,-1, -1, 1,-1, -1,-1,-1, 1,-1,-1
};
float colors[]={
1,0,0, 1,0,0, 1,0,0, 1,0,0,
0,1,0, 0,1,0, 0,1,0, 0,1,0,
0,0,1, 0,0,1, 0,0,1, 0,0,1,
1,1,0, 1,1,0, 1,1,0, 1,1,0,
1,0,1, 1,0,1, 1,0,1, 1,0,1,
0,1,1, 0,1,1, 0,1,1, 0,1,1
};
Uint32 timerfunc(Uint32 interval,void* param){
rquad+=0.8f;
return interval;
}
void initGL(){
glViewport(0,0,640,480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,640.0/480.0,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glShadeModel(GL_SMOOTH);
glClearColor(0,0,0,0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glGenBuffers(1,&vertexBuffer);
glGenBuffers(1,&colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glBufferData(GL_ARRAY_BUFFER,72*sizeof(float),vert,
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,colorBuffer);
glBufferData(GL_ARRAY_BUFFER,72*sizeof(float),colors,
GL_STATIC_DRAW);
}
void drawGLScene(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0,0,-6);
glRotatef(rquad,1,1,0.5);
glBindBuffer(GL_ARRAY_BUFFER,vertexBuffer);
glVertexPointer(3,GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER,colorBuffer);
glColorPointer(3,GL_FLOAT,0,0);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS,0,24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0);
SDL_GL_SwapBuffers();
}
int main(int argc, char** argv){
int videoFlags=0;
videoFlags|=SDL_OPENGL;
videoFlags|=SDL_GL_DOUBLEBUFFER;
videoFlags|=SDL_HWPALETTE;
videoFlags|=SDL_HWSURFACE;
videoFlags|=SDL_HWACCEL;
int done=0;
SDL_Event event;
const SDL_VideoInfo* videoInfo;
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER);
videoInfo=SDL_GetVideoInfo();
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDL_WM_SetCaption("Hello OpenGL!",NULL);
surface=SDL_SetVideoMode(640,480,32,videoFlags);
initGL();
SDL_AddTimer(10,timerfunc,NULL);
while(!done){
while(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT){
done=1;
}
}
drawGLScene();
}
SDL_Quit();
return 0;
}
In addition, I compile my code using the following command: gcc sdlogl.c -g -Wall -lSDL -lGL -lGLU
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到的这些警告告诉您,上述函数的原型尚未正确声明,并且编译器假定默认的 C 函数签名
int ()
。尽管包含
glext.h
不会为您提供声明(出于各种原因),但会为您提供适当的 typedef 来自行引入这些标识符。但随着时间的推移,它会变得乏味。这就是实现包装器库的原因。我推荐 GLEW 或 GLEE。使用 GLEE,整个事情可以归结为包含GL/glee.h
而不是GL/gl.h
。Those warnings you get tell you, that the mentioned functions' prototypes have not been properly declared and the compiler is presuming the default C function signature
int ()
.Including
glext.h
will not give you the declarations though (for various reasons), but gives you proper typedefs to introduce those identifiers yourself. It gets tedious over time though. That's why wrapper libraries have been implemented. I recomment GLEW or GLEE. Using GLEE the whole thing boils down to includingGL/glee.h
instead ofGL/gl.h
.这些扩展函数必须使用
glXGetProcAddress
或使用 GLEW (更简单)加载。我必须警告你,如果你不这样做,你的程序将会崩溃。Those extension functions must be loaded using
glXGetProcAddress
or using GLEW (easier). I must warn you that your program will crash if you don't do so.