c - Ubuntu 中的 OpenGL(C 语言)说事情未声明

标签 c opengl ubuntu gcc undeclared-identifier

所以我刚开始学习 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/

相关文章:

php - 从php在webroot之外执行shell脚本

c - 文本文件撇号麻烦

objective-c - ObjC 访问 C 函数中的属性

c - linux 中是否有一个命令来查明库是在 32 位还是 64 位模式下构建的

objective-c - 如何在屏幕上绘制矩形。 NSOpenGLContext 与透明 NSWindow + 自定义 NSView

c++ - Opengl统一行为

python - 无法访问 docker 容器 Socket 挂起错误

c - 为什么在 BST 中搜索比二进制搜索算法更快

java - 调整大小后剪切 OpenGL/GLSL 视口(viewport)

通过 Internet 连接时,Apache 尝试连接到本地网络