所以我刚开始学习 OpenGL,我正在使用 C 语言学习 Ubuntu。
我从我的讲师笔记中做了几个例子,它们起作用了,但是这个给我错误。
callbackexample.c: In function ‘main’:
callbackexample.c:17:18: error: ‘displays’ undeclared (first use in this function)
callbackexample.c:17:18: note: each undeclared identifier is reported only once for each function it appears in
依此类推我文件中的每个方法。 我逐字逐句地按照他的笔记进行,我得到了这个,所以我不确定出了什么问题。
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define DEG_TO_RAD 0.017453
int singleb, doubleb; //window ids
GLfloat theta = 0.0;
int main(int argc, char **argv){
glutInit(&argc, argv);
//create single buffered window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
singleb = glutCreateWindow("single_buffered");
glutDisplayFunc(displays);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutKeyboardFunc(mykey);
//create double buffered window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(400,0); //create window to the right
doubleb = glutCreateWindow("double_buffered");
glutDisplayFunc(displayd);
glutReshapeFunc(myReshape);
glutIdleFunc(spinDisplay);
glutMouseFunc(mouse);
glutCreateMenu(quit_menu);
glutAddMenuEntry("quit", 1);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
void displays() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f ( cos(DEG_TO_RAD * theta),
sin(DEG_TO_RAD * theta));
glVertex2f ( -sin(DEG_TO_RAD * theta),
cos(DEG_TO_RAD * theta));
glVertex2f ( -cos(DEG_TO_RAD * theta),
-sin(DEG_TO_RAD * theta));
glVertex2f ( sin(DEG_TO_RAD * theta),
-cos(DEG_TO_RAD * theta));
glEnd();
glFlush();
}
这只是 main 方法的一些代码,以及它之后出现错误的第一个方法。 undeclared 我想这意味着方法没有声明,但我遵循了他的代码,所以我不确定
最佳答案
在声明它之前,您尝试在 main 函数中使用 displays
方法。您需要将整个函数移动到主函数之前,或者将 stub 添加到顶部:
void displays();
int main(int argc, char **argv){
...
}
void displays(){
...
}
C 按照它看到的顺序解析所有内容 - 你不能使用一个没有完整声明的方法,或者至少有一个声明它会在某个时候存在。
关于您的评论: 找不到 -l* 你得到的东西意味着你要么没有安装 OpenGL 的开发库,要么设置得很奇怪 - 这就是说它找不到要链接的库文件。
此外,mykey
问题意味着您还没有声明 mykey
函数,或者没有按照原型(prototype)声明它:
void mykey(unsigned char key, int x, int y)
关于c - Ubuntu 中的 OpenGL(C 语言)说事情未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824353/